Results 1 to 7 of 7
- 06-13-2011, 06:07 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
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:
---------------------------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:
---------------------------
---------------------------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:
---------------------------
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!Last edited by borfoo3; 06-13-2011 at 10:28 AM. Reason: Added the error lines at the appropriate location as requested
- 06-13-2011, 07:02 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Where is line 43 of Pet.java and line 63 of Assignment4.java?
What is you input for this parameters?
- 06-13-2011, 09:34 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
I tried these inputs as my test case when running Assignment4.java:
A
Chloe
ChihuahuaDog
4
7
2006
BeverlyHills
D
Q
- 06-13-2011, 09:58 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
You just declare BirthInfo but did not initialize it
- 06-13-2011, 10:13 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
See this:
Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
Please read the link especially the topic about "Creating Object"
- 06-13-2011, 10:33 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
ohhh.. i see, you're right. It's working now. Thanks for the help!
- 01-23-2012, 07:11 AM #7
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
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.
Similar Threads
-
Unresolved Compilation Problems
By eck0 in forum New To JavaReplies: 1Last Post: 05-19-2011, 12:17 PM -
Unresolved compilation problem
By ls7897 in forum New To JavaReplies: 9Last Post: 03-09-2011, 01:42 AM -
Working on a menu program...using exceptions
By Nightwarrior in forum New To JavaReplies: 0Last Post: 04-16-2009, 04:40 AM -
Need help putting Exceptions in my program.
By Loop in forum New To JavaReplies: 6Last Post: 10-13-2008, 11:48 PM -
Unresolved compilation problem
By mew in forum New To JavaReplies: 2Last Post: 12-30-2007, 07:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks