Results 1 to 7 of 7
- 06-26-2011, 06:19 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 1
- Rep Power
- 0
Need help with Null Pointer Exception (program using inheritance)
I am writing a program for class. The specifications for this program was that the given
TestSmallRoom.java coding MUST NOT CHANGE. The Person class was written by myself a couple of weeks ago. We were instructed to make 3 new classes, Tutor ( extends Person ), Student ( extends Person ), and SmallRoom, which represents a small room in which only 1 person can occupy at a time. The output of TestSmallRoom.java should output various statistics of objects that are changed throughout the program's execution.
I am not understanding why i am getting a null pointer exception, yet alone having the error point to coding that works.
Can anyone help me with this?
Java Code:public class Person { private String personName; private int dollars; private boolean happy; //constructor initializes the Person's money public Person( String name, int money, boolean bool ) { setPersonName(name); if( money >= 0 ) { setDollars(money); } else { setDollars(50); } setHappy(bool); }//end constructor //method to set personName public void setPersonName( String name ) { personName = name; } //method to get personName public String getPersonName() { return personName; } //method to set personMoney public void setDollars( int money ) { if( money >= 0 ) { dollars = money; } else { dollars = 50; } } //method to get personMoney public int getDollars() { return dollars; } //method to set happy public void setHappy( boolean bool ) { happy = bool; } //method to get happy public boolean getHappy() { return happy; } //method to determine if someone is happy public boolean isHappy() { return ( getHappy() ? true : false ); } //public method to spend public void spend( int amtSpent ) { if( amtSpent >= 0 ) { if( amtSpent <= getDollars() ) { setDollars( getDollars() - amtSpent ); }//end if else { setDollars( getDollars() - getDollars() ); }//end else }//end if }//end method spend //method to determine if person can afford to spend public boolean canAfford( int amount ) { return ( ( getDollars() >= amount ) ? true : false ); }//end method canAfford //method to earn money public void earned( int amtEarned ) { if( amtEarned >= 0 ) { setDollars( getDollars() + amtEarned ); } } //method to say something public void says() { if( getHappy() == true ) { System.out.println( "My name is " + getPersonName() + " and I am doing great!" ); } else { System.out.println( "My name is " + getPersonName() + " and I could be better." ); } } //method toString overrides superclass toString method. @Override public String toString() { return String.format( "%s %s %s %s", getPersonName(), "has", getDollars(), "dollars"); } }// end class Person
Java Code:// Student.java // The Student class is just made up of a constructor and a toString method. class Student extends Person { private String studentMajor; // keep track of the student's major // constructor public Student( String name, int money, boolean bool, String major ) { super( name, money, bool ); studentMajor = major; }// end Student constructor // toString method @Override // indicates this method overrides a superclass method public String toString() { return String.format( "%s %s, %s %s", "student", super.toString(), "major is", studentMajor ); } }
Java Code:public class Tutor extends Person { private double hourlyRate; // the hourly rate that the tutor charges // constructor public Tutor( String name, int money, boolean bool, double rate ) { super( name, money, bool ); if( rate >= 0.0 ) { hourlyRate = rate; } else { hourlyRate = 10.0; } }// end Tutor constructor // toString method @Override // indicates this method overrides a superclass method public String toString() { return String.format( "%s %s %s %s %s $%.2f %s", "tutor", super.getPersonName(), "has", super.getDollars(), "dollars and charges", hourlyRate, "per hour" ); } }
Java Code:// SmallRoom.java // In addition to constructor(s) and toString methods, the SmallRoom class // will have the following methods: isEmpty, isOccupied, enter, leave public class SmallRoom { private Person occupant; // make up a name for the person who is occupying the room private String room; // this field represents the name of the room // constructor public SmallRoom( String roomNumber ) { room = roomNumber; } // toString method @Override // indicates this method overrides a superclass method public String toString() { if( room.isEmpty() ) { return String.format( "%s %s %s", "room", room.toString(), "is empty" ); } else { return String.format( "%s %s %s %s", "room", room.toString(), "is occupied by", occupant.toString() ); } } // isEmpty method public boolean isEmpty() { if( occupant == null ) return true; else return false; } // isOccupied method public boolean isOccupied() { if( occupant != null ) return true; else return false; } // enter method public void enter( Person person ) { occupant = person; } // leave method }
Java Code:public class TestSmallRoom { public static void main(String [] args) { Tutor alan = new Tutor("Jackson", 100, true, 26.00); Tutor carol = new Tutor("Bennett", 100, true, -32.00); // bad rates default to $10.00 Student happy = new Student("Smiles", 500, true, "Software Engineering" ); Student ming = new Student("Li", 75, false, "Accounting"); Student prakash = new Student("Ramanan", 110, false, "Nursing" ); Student bill = new Student("Clinton", 5000, true, "Poli Sci"); alan.earned(52); bill.spend(3300); System.out.println(alan); System.out.println(carol); System.out.println(happy); System.out.println(ming); SmallRoom room1 = new SmallRoom("F105"); SmallRoom room2 = new SmallRoom("G110"); SmallRoom room3 = new SmallRoom("H300"); if (room1.isEmpty()) room1.enter(alan); if (room2.isEmpty()) room2.enter(prakash); System.out.println(room1); System.out.println(room2); System.out.println(room3); // this line produces a null pointer exception, BUT // the exception shows the coding for the code // that did print correctly. // THE FOLLOWING CODE IS COMMENTED OUT BECAUSE THE PROGRAM IS UNFINISHED // room1.enter(bill); // rejected since already occupied // Person p = null; // The "leave" method set the room to empty and returns a // reference to whoever was in the room. Below, the reference // p will point to student prakash and room2 becomes empty. // A null reference is returned if the room was already empty // when the leave method was called. // if (room2.isOccupied()) // p = room2.leave(); // System.out.println("Just left room 2: " + p); // System.out.println("Room2 is now: " + room2); // SmallRoom room4 = new SmallRoom("L002"); // room3.enter(bill); // room4.enter(room3.leave()); // Bill moves from room3 to room4 // System.out.println("AT THE END:"); // System.out.println(room1); // System.out.println(room2); // System.out.println(room3); // System.out.println(room4); } }
- 06-26-2011, 06:38 PM #2I am not understanding why i am getting a null pointer exception,
It has the line number where the error occurred.
If you can find that line in your code, see what variable is null and back track to find out why.
If you can not tell which one is null, Add a println statement before the statement to show all the variables' values used on that line.
- 06-26-2011, 06:39 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
You've posted tons of code without the full stack trace. Your best bet to help you get advice is to do one or both of the following: 1) post an SSCCE 2) post the full stack trace of the exception whose line numbers line up with the posted code
-
You're calling isEmpty on the room String which is the wrong entity for that method. You don't care if the String name for the room has text or not, but rather if the room is occupied or not. Perhaps you should call this method on another object (or another redundant method in the class). But I also agree, when you have an error that you need help with, please post the entire exception message and indicate which lines are throwing the exception.
Last edited by Fubarable; 06-26-2011 at 07:32 PM.
- 06-27-2011, 08:25 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Here is the error he is getting
symbol : method leave()
location: class SmallRoom
p = room2.leave();
^
TestSmallRoom.java:55: cannot find symbol
symbol : method leave()
location: class SmallRoom
room4.enter(room3.leave()); // Bill moves from room3 to room4
- 06-27-2011, 08:32 AM #6
Aha!
I knew I wasn't going crazy.Last edited by Junky; 06-28-2011 at 12:39 AM.
- 06-27-2011, 08:40 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
Help with my Tic Tac Toe program-null pointer exception
By TheBadBoy in forum New To JavaReplies: 4Last Post: 06-11-2011, 01:35 AM -
Null pointer exception
By jessie in forum New To JavaReplies: 5Last Post: 02-08-2011, 02:58 PM -
Null Pointer exception
By diegoyj in forum New To JavaReplies: 7Last Post: 01-29-2010, 04:17 PM -
null pointer exception
By anthonym2121 in forum New To JavaReplies: 7Last Post: 04-06-2009, 03:25 AM -
Null pointer exception
By Stephenmak in forum New To JavaReplies: 5Last Post: 04-01-2009, 02:17 PM
Bookmarks