1

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.

Scarlet
  • 23
  • 5
  • Possible duplicate of [Java null check why use == instead of .equals()](http://stackoverflow.com/questions/4501061/java-null-check-why-use-instead-of-equals) – Hypaethral Apr 07 '16 at 18:27
  • Check out http://stackoverflow.com/questions/4501061/java-null-check-why-use-instead-of-equals , I think you'll find it helpful. If you use an instance-based equality method when you don't have an instance, you'll get a NullPointerException: "null.equals(null)" doesn't make sense, right? Use ==, instead. – Hypaethral Apr 07 '16 at 18:27

1 Answers1

1

The expression northExit.equals(null) (as one example) will not work as you think it will.

Think of it this way: the northExit. bit implies a dereferencing of the northExit reference to find what exactly it points to. Such a dereference attempt when it's null will give you the exception you're seeing.

The correct way to check if a value is null is to use reference equality(a),along the lines of:

if ((northExit != null) && (! northExit.equals(""))) ...

(a) When teaching, I've often found borrowing a couple of five dollar notes from students and explain it thus.

In terms of reference equality ==, they're different since they're actually distinct physical items. In terms of content or value equality .equals(), they are identical.

Then I pocket the ten bucks and hope they forget about it by the end of the lesson, a nice little supplement to my earnings :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953