Unresolved Exceptions in my program
Hello Everyone.
Here's the deal. We were given a completed driver program (called Assignment4.java) from our prof. and we were asked to complete the required Pet.java and BirthInfo.java files.
So far, I've finished the two files but I'm having difficulties running the program as a whole and I'm getting these exceptions:
Exception in thread "main" java.lang.NullPointerException
at Pet.setBirthInfo(Pet.java:43)
at Assignment4.main(Assignment4.java:63)
This is the Assignment4.java:
Code:
import java.io.*; //to use InputStreamReader and BufferedReader
import java.util.*;
public class Assignment4
{
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);
// matches one of the case statement
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();
[B] pet1.setBirthInfo(date, month, year, place); //exception in Assignment4 (line63)
[/B]
break;
case 'D': //Display Pet
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");
}
}
---------------------------
This is Pet.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");
}
}
---------------------------
This is BirthInfo.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 suspect that I did something wrong in the setBirthInfo method in Pet.java. However, I'm really a beginner at java so I might have messed up somewhere else..
Thanks!
Re: Unresolved Exceptions in my program
We have the same assignemt, Did you get this to run after initializing BirthInfo?
I get the same NullPointException, expect earlier on in Assignment4 when it is instantiating a new pet (line 16). Any ideas on what is wrong with my Pet class? It is identical to this one, with Birth Info initialized.