I have researched this extensively and found answers for checking if Strings are not null, but not for when checking if a class I've instantiated is null. This is a class instantiated by the another class that keeps a list of all Rooms, just like the Cave Adventure game. Here is the code:
public class Room
{
private String description;
private Room northExit;
private Room southExit;
private Room eastExit;
private Room westExit;
/**
* Create a room described "description". Initially, it has
* no exits. "description" is something like "a kitchen" or
* "an open court yard".
* @param description The room's description.
*/
public Room(String description)
{
this.description = description;
}
/**
* Define the exits of this room. Every direction either leads
* to another room or is null (no exit there).
* @param north The north exit.
* @param east The east east.
* @param south The south exit.
* @param west The west exit.
*/
public void setExits(Room north, Room east, Room south, Room west)
{
if(north != null)
northExit = north;
if(east != null)
eastExit = east;
if(south != null)
southExit = south;
if(west != null)
westExit = west;
}
/**
* @return The description of the room.
*/
public String getDescription()
{
return description;
}
public Room getExit(String direction)
{
if(direction.equals("north")) {
return northExit;
}
if(direction.equals("east")) {
return eastExit;
}
if(direction.equals("south")) {
return southExit;
}
if(direction.equals("west")) {
return westExit;
}
return null;
}
public String getExitString() {
if (!northExit.equals(null) && !northExit.equals(""))
return "north ";
if (!eastExit.equals(null) && !eastExit.equals(""))
return "east ";
if (!southExit.equals(null) && !southExit.equals(""))
return "south ";
if (!westExit.equals(null) && !westExit.equals(""))
return "west ";
else {
System.out.println("There are no doors!");
return null;
}
}
}
I end up getting a NullPointerException when it reaches the getExitString() method.
I've been working on this for many many hours and I'm at the limit of my frustrations currently, any help will be GREATLY appreciated.