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)
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");
}
}
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");
}
}
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)
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:
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).
Re: I need help with a simple error
So the solution is: create a Birth object and assign it to your birth variable before trying to use the variable.
Make sense?