Results 1 to 10 of 10

Thread: object null!

  1. #1
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default object null!

    Hi here is my code, as you can see I'm trying to print out the object which I created but every time the output is null. I've been trying to a while to get this working and I just cant figure it out. I want to be able to print out the values of the object which I created


    Java Code:
    /**
     * Write a description of class Player here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Player
    {
        // instance variables - replace the example below with your own
        String name;
        int age;
        Player player1;
    
        /**
         * Constructor for objects of class Player
         */
        public Player(String name, int age)
        {
            // initialise instance variables
            this.name = name;
            this.age = age;
            
            System.out.println(player1);
        }
        
        public static void main(String[] args){
            
            Player player1 = new Player("Mary", 16);
            
            
            
        }
    
        /**
         * An example of a method - replace this comment with your own
         * 
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         */
        public void sampleMethod(int y)
        {
            // put your code here
            
        }
    }

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: object null!

    You did not create the object "player1" at the point where you want to print it... to create it you have to assign player1 to a "new" object:
    player1 = new Player(...);
    and AFTER that you print it.

    BUT: You are having TWO variables, one of which you assign a new object and the other one which is null by default:
    Player player1 = new Player("Mary", 16); // first one...
    Player player1; // in the class declaration is the second one... you do not need this one! In fact it is wrong.

    The next is that a simple println on player1 will not give you what you want. To have that you need to "@Override" the method toString() of your object.
    This is a perfect example of why variables in different scopes should never be named the same as you are assuming in your constructor that you are printing the local variable in "main()" but in fact you print the class attribute... ;)
    Last edited by Sierra; 12-20-2012 at 04:02 PM.
    I like likes!

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: object null!

    You are referring to the System.out.println() in the Player's constructor that print out null right? In this case the player1 is indeed referring to a null. No any object was created that referred by this variable.

    You have a player variable that you've declared and instantiated in the main() method. But this variable is local to the main() method. It is different to the one you've define in the global scope / instance variable.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: object null!

    I's like to add that objects are never null. References may be null, if declared but not assigned, or if explicitly assigned to null.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default Re: object null!

    I still don't understand, I have instantiated my class variables in the constructor and then I created the object in the main method and printed. the output is now "Player@66fcc070", what does this mean?


    Java Code:
    public class Player
    {
        // instance variables - replace the example below with your own
        String name;
        int age;
        /**
         * Constructor for objects of class Player
         */
        public Player(String name, int age)
        {
            // initialise instance variables
            this.name = name;
            this.age = age;    
        }
        
        public static void main(String[] args){
            
            Player player1 = new Player("Mary", 16);
            
            System.out.println(player1);
            
        }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: object null!

    A Player object doesn't know how to construct a String representation of itself so it inherits the toString() method from the Object class; that method produces the strange looking String. Define a toString() method in your Player class (it should return something readable) and you're done.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default Re: object null!

    ok thanks, also, is it possible to take parameters by reading from a keyboard input?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: object null!

    Quote Originally Posted by joe75 View Post
    ok thanks, also, is it possible to take parameters by reading from a keyboard input?
    What do you mean?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default Re: object null!

    it doesnt matter, I have found out. basically, i wanted to create a new player only if the user wanted to on prompt

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: object null!

    Quote Originally Posted by joe75 View Post
    it doesnt matter, I have found out. basically, i wanted to create a new player only if the user wanted to on prompt
    Good; it would be nice if you posted your solution for other readers of this forum.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 13
    Last Post: 09-04-2012, 06:19 PM
  2. Null Object
    By Dex in forum New To Java
    Replies: 2
    Last Post: 03-25-2012, 04:41 PM
  3. Null Pointer Exception for formatter object
    By andre1011 in forum Networking
    Replies: 0
    Last Post: 03-28-2009, 10:51 PM
  4. How setting an Object to null help Garbage Collection?
    By piyu.sha in forum Advanced Java
    Replies: 3
    Last Post: 10-06-2008, 07:35 AM
  5. what happen if an object set to null
    By SaYuNaRa in forum New To Java
    Replies: 5
    Last Post: 04-17-2008, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •