Results 1 to 3 of 3
  1. #1
    grim is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default I need help with a simple error

    I'm getting these exceptions:

    Exception in thread "main" java.lang.NullPointerException
    at Pet.setBirthInfo(Pet.java:43)
    at Assignment4.main(Assignment4.java:63)
    Java Code:
    public class Assignment
     {
      public static void main (String[] args)
       {
           // local variables, can be accessed anywhere from the main method
           char input1 = 'Z';
           String inputInfo;
           String name, type, place;
           int date, month, year;
           String line = new String();
    
           // instantiate a Pet object
           Pet pet1 = new Pet();
    
           printMenu();
    
           //Create a Scanner object to read user input
           Scanner scan = new Scanner(System.in);
    
    
           do  // will ask for user input
            {
             System.out.println("What action would you like to perform?");
             line = scan.nextLine();
    
             if (line.length() == 1)
              {
               input1 = line.charAt(0);
               input1 = Character.toUpperCase(input1);
    
               switch (input1)
                {
                 case 'A':   //Add Pet
                   System.out.print("Please enter the pet information:\n");
                   System.out.print("Enter a pet name:\n");
                   name = scan.nextLine();
                   pet1.setPetName(name);
    
                   System.out.print("Enter its type:\n");
                   type = scan.nextLine();
                   pet1.setType(type);
    
                   System.out.print("Enter its birth date:\n");
                   date = Integer.parseInt(scan.nextLine());
                   System.out.print("Enter its birth month:\n");
                   month = Integer.parseInt(scan.nextLine());
                   System.out.print("Enter its birth year:\n");
                   year = Integer.parseInt(scan.nextLine());
                   System.out.print("Enter its birth place:\n");
                   place = scan.nextLine();
                   pet1.setBirthInfo(date, month, year, place);
    
                   break;
                 case 'D':   //Display course
                   System.out.print(pet1);
                   break;
                 case 'Q':   //Quit
                   break;
                 case '?':   //Display Menu
                   printMenu();
                   break;
                 default:
                   System.out.print("Unknown action\n");
                   break;
                }
              }
             else
              {
               System.out.print("Unknown action\n");
              }
            } while (input1 != 'Q' || line.length() != 1);
       }
    
      /** The method printMenu displays the menu to a user **/
      public static void printMenu()
       {
         System.out.print("Choice\t\tAction\n" +
                            "------\t\t------\n" +
                            "A\t\tAdd Pet\n" +
                            "D\t\tDisplay Pet\n" +
                            "Q\t\tQuit\n" +
                            "?\t\tDisplay Help\n\n");
      }
    }
    Java Code:
    public class Pet 
    {
     
        private String petName;
        private String type;
        private BirthInfo birth;
         
         
        public Pet() //constructor initializing both Strings to "?"
        {
            petName="?";
            type="?";
        }
         
        public String getPetName()
        {
            return petName;
        }
         
        public String getType()
        {
            return type;
        }
         
        public BirthInfo getBirthInfo()
        {
            return birth;
        }
         
        public void setPetName(String pName)
        {
            petName = pName;
        }
         
        public void setType(String pType)
        {
            type = pType;
        }
         
        public void setBirthInfo(int date, int month, int year, String place)
        {
            [B]birth.setDate(date);               //Exception here: pet.java line 43   [/B]
            birth.setMonth(month);
            birth.setYear(year);
            birth.setPlace(place);
        }
         
        public String toString()
        {
             
            return ("\nPet Name:\t\t" + petName + "\nType:\t\t\t" +
                    type + "\nBirth Information:\t"+ birth +"\n\n");
        }
         
    }
    Java Code:
    public class BirthInfo
    {
    *
    ****private int date;
    ****private int month;
    ****private int year;
    ****private String place;
    *****
    ****public BirthInfo()
    ****{
    ********date=0;
    ********month=0;
    ********year=0;
    ********place = "?";
    ****}
    *****
    ****public int getDate()
    ****{
    ********return date;
    ****}
    *****
    ****public int getMonth()
    ****{
    ********return month;
    ****}
    *****
    ****public int getYear()
    ****{
    ********return year;
    ****}
    *****
    ****public String getPlace()
    ****{
    ********return place;
    ****}
    *****
    ****public void setDate(int date1)
    ****{
    ********date = date1;
    ****}
    *****
    ****public void setMonth(int month1)
    ****{
    ********month = month1;
    ****}
    *****
    ****public void setYear(int year1)
    ****{
    ********year = year1;
    ****}
    *****
    ****public void setPlace(String place1)
    ****{
    ********place = place1;
    ****}
    *****
    ****public String toString()
    ****{
    ********return ("Date " + date + "/Month "+month+"/Year "+ year +"/Place " +place);
    ****}
    }
    I'm getting these exceptions:

    Exception in thread "main" java.lang.NullPointerException
    at Pet.setBirthInfo(Pet.java:43)
    at Assignment4.main(Assignment4.java:63)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I need help with a simple error

    Hello and welcome to the Java-forums.org!

    Can you edit your post above and show us which lines are causing the errors? Often it is best to put comments in your code showing us this, and usually finding out which line causes the error leads directly to its solution as you can see which variables are dereferenced on that line (have methods called or fields accessed), and then backtrack in your code to see why those variables are null.

    Also, please get rid of the distracting **** Strings in your bottom class as it makes the code difficult to read and impossible to use.

    thanks and best of luck.

    Edit:

    Oh, I see, you've done the first request -- good show!

    here:

    Java Code:
        public void setBirthInfo(int date, int month, int year, String place)
        {
            [B]birth.setDate(date);               //Exception here: pet.java line 43   [/B]
            birth.setMonth(month);
            birth.setYear(year);
            birth.setPlace(place);
        }
    So the variable in question is birth. Now check that code and see where birth is assigned to an actual object (hint, it's not).

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Simple Method Gives Me an Error
    By grahamcrackeh in forum New To Java
    Replies: 6
    Last Post: 06-10-2011, 04:00 PM
  2. Simple Protocol Error!!
    By hamad3914 in forum Networking
    Replies: 4
    Last Post: 04-15-2011, 09:07 PM
  3. 30 line simple gui, simple error, need help
    By cc11rocks in forum New To Java
    Replies: 6
    Last Post: 02-05-2011, 08:33 PM
  4. Simple Error ???
    By darkblue24 in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 08:15 AM
  5. compiling error should be a simple fix.
    By bottlecap in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 06:07 AM

Tags for this Thread

Posting Permissions

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