Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: will u tell me why i am getting output "null"

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post will u tell me why i am getting output "null"

    public class thread {
    public static void main(String[] args) {

    mythreadprog my1 = new mythreadprog("demo");
    // mythreadprog my2 = new mythreadprog("demo2");

    }
    }



    class personal_details
    {
    String name,mname,lname,course;
    int roll_no;

    void add_details()
    {
    name= "chirag";
    mname="n";
    lname="Shah";
    roll_no= 136;
    course = "Msc";

    }
    void print_details()
    {

    System.out.println(name);
    System.out.println(mname);
    System.out.println(lname);
    System.out.println(roll_no);
    System.out.println(course);

    }
    }




    class mythreadprog implements Runnable
    {
    personal_details p1= new personal_details();
    String threadname;
    Thread t;
    mythreadprog (String name)
    {
    threadname = name;
    t= new Thread(this,"Mydemo");
    //System.out.println(t);
    t.start();

    }


    public void run()
    {

    p1.print_details();

    }
    }


  2. #2
    Junior Member Tjones787's Avatar
    Join Date
    Jan 2014
    Location
    Lagos, Nigeira
    Posts
    16
    My Mood
    Asleep
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: will u tell me why i am getting output "null"

    Your datafield in the personal details class where not initialised.
    And your program did not call the method you use to initialise the variable. That is the add_detail() method in the personal details class.

    That's why u receive a null output. Since there are no object in the your details you asked the program to print.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: will u tell me why i am getting output "null"

    yes, i got it correctly now.
    Thank you.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: will u tell me why i am getting output "null"

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

Similar Threads

  1. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 10th, 2023, 03:11 AM
  2. null pointer exception in "rs.next()"
    By Nimisha in forum Exceptions
    Replies: 2
    Last Post: July 2nd, 2013, 08:57 AM
  3. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  4. [SOLVED] "null" in my output :( Scanner problem?
    By crys in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2012, 09:05 PM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM