Results 1 to 7 of 7
  1. #1
    kali1980 is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default 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);
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    I am not understanding why i am getting a null pointer exception,
    Please copy and paste here the full text of the error message.
    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.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    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

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

    Default

    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.

  5. #5
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    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

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Aha!

    I knew I wasn't going crazy.
    Last edited by Junky; 06-28-2011 at 12:39 AM.

  7. #7
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2011, 01:35 AM
  2. Null pointer exception
    By jessie in forum New To Java
    Replies: 5
    Last Post: 02-08-2011, 02:58 PM
  3. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 04:17 PM
  4. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 03:25 AM
  5. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:17 PM

Posting Permissions

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