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 7 of 7

Thread: Cannot print name, getting null error.

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Cannot print name, getting null error.

    Firstly thanks taking the time to help me out.

    I can compile and run this program but where I want to print the Dogs name and age (with the toString method) it prints only the age, returning "null" for name.

    //Dog.java
    //represents a dogs age and name and contains methods to do several
    //things.
     
    public class Dog
    {
      private String name;
      private int age;
     
      //constructor: initializes the instance data name and age:
      public Dog ()
      {
      age = 14;
      }
     
      //getter
      public int getAge ()
      {
        return age;
      }
     
      //setter
      public void setAge (int value)
      {
        value = age;
      }
     
      //getter
      public String getName ()
      {
        return name;
      }
     
      //setter
      public void setName (String words)
      {
        name = words;
      }
     
      //toString method
      public String toString()
      {
        return name + "\t" + age;
      }
    }

    and the driver class:

    //Kennel.java
    //main method instantiates and updates several Dog objects.
     
    public class Kennel
    {
      public static void main (String[] args)
      {
     
      Dog Scruffy, Jade;
     
      Scruffy = new Dog ();
     
      Scruffy.getName();
     
      System.out.println ("Scruffys age in human years: " + 
      Scruffy.getAge());
     
      System.out.println (Scruffy);
      }
    }

    and when running the program i get:
    craig@craig-laptop:~/Documents/panda/java2$ java Kennel
    Scruffys age in human years: 14
    null	14
    I want the dogs name to show as 'Scruffy'.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot print name, getting null error.

    Is the variable name in the Dog class ever assigned a value? Its default value is null. If the code doesn't assign it a value, its value will be null. Look at the code to see how it can be made to assign a value to name. Look for a statement that starts with:
    name =


    BTW The methods whose names begin with get return values that should be assigned to a variable. Otherwise the returned value is lost:
      varNameHere = refToClass.getSomeValue(); //  save value returned by method
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Cannot print name, getting null error.

    I've tried editing the constructor to this:
      //constructor: initializes the instance data name and age:
      public Dog ()
      {
      name = Scruffy;
      age = 14;
      }
    But it doesnt compile:
    craig@craig-laptop:~/Documents/panda/java2$ javac Dog.java
    Dog.java:13: error: cannot find symbol
      name = Scruffy;
             ^
      symbol:   variable Scruffy
      location: class Dog
    1 error

    And i've also tried putting the name in at with the data declarations:
    public class Dog
    {
      private String name = Scruffy;
      private int age;
    But that gives an error too:
    craig@craig-laptop:~/Documents/panda/java2$ javac Dog.java
    Dog.java:7: error: cannot find symbol
      private String name = Scruffy;
                            ^
      symbol:   variable Scruffy
      location: class Dog
    Dog.java:13: error: cannot find symbol
      name = Scruffy;
             ^
      symbol:   variable Scruffy
      location: class Dog
    2 errors

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot print name, getting null error.

    You have coded Scruffy as a variable. If you want the value of name to be "Scruffy" you need to enclose it in "s.

    What if you have another dog named Spot? How would you create a Dog object with a name of Spot?
    Is there a method in the Dog class that would help?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Cannot print name, getting null error.

    Ok I've got scruffy sorted
    Now im trying to make a new dog Spot by doing this:

     Spot = new Dog ();
     Spot.setName (Spot);

    but get error:
    craig@craig-laptop:~/Documents/panda/java2$ javac Kennel.java
    Kennel.java:19: error: method setName in class Dog cannot be applied to given types;
      Spot.setName (Spot);
          ^
      required: String
      found: Dog
      reason: actual argument Dog cannot be converted to String by method invocation conversion
    1 error

    I know im doing something stupid here but I dont know what. How do should I create a new Dog object and declare its name as spot?

    --- Update ---

    I have this now at the top of the Dog class:
    public class Dog
    {
      private String name;
      private int age;
     
      //constructor: initializes the instance data name and age:
      public Dog ()
      {
      name = "Scruffy";
      age = 14;
      }

    But I want the name variable to change for every new Dog object I create. Is that going to happen or is it going to try and call every new dog "Scruffy" ?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot print name, getting null error.

    s it going to try and call every new dog "Scruffy" ?
    The constructor sets the name to "Scruffy" for each instance of the object that is created. To give the Dog object a different name you need to call the set method.
    Another choice is to add another constructor to the Dog class that has the name and age as arguments.
    The constructor can take the values of the args and use them to give values to the class variables.

    See the tutorial: http://docs.oracle.com/javase/tutori...structors.html

    required: String
    found: Dog
    A String is enclosed in "s: "Spot". Without the "s, the compiler treats Spot as the name of a variable.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Cannot print name, getting null error.

    Ok I have this sorted now Norm. Thanks for your help.

Similar Threads

  1. error in print image onto applet
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2012, 08:17 PM
  2. Null Error
    By Nessera in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2011, 01:46 PM
  3. Null Pointer Exception error
    By Keitho55 in forum Exceptions
    Replies: 3
    Last Post: November 18th, 2011, 03:29 AM
  4. Null or not an object error
    By James W in forum Other Programming Languages
    Replies: 4
    Last Post: November 1st, 2011, 01:06 PM
  5. NULL POINTER EXCEPTION error
    By beefwithrice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2011, 06:26 AM