检查Bluej(Java)是否不工作

检查Bluej(Java)是否不工作,java,null,bluej,Java,Null,Bluej,我对此进行了广泛的研究,找到了检查字符串是否为null的答案,但没有找到检查我实例化的类是否为null的答案。这是一个由另一个类实例化的类,该类保存所有房间的列表,就像洞穴探险游戏一样。代码如下: public class Room { private String description; private Room northExit; private Room southExit; private Room eastExit; private Roo

我对此进行了广泛的研究,找到了检查字符串是否为null的答案,但没有找到检查我实例化的类是否为null的答案。这是一个由另一个类实例化的类,该类保存所有房间的列表,就像洞穴探险游戏一样。代码如下:

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

    }
}
当它到达getExitString()方法时,我最终得到一个NullPointerException


我已经为此工作了很多个小时,目前我的挫折已经到了极限,任何帮助都将不胜感激。

表达式
northExit.equals(null)
(作为一个例子)不会像您想象的那样工作

可以这样想:
northExit。
位意味着取消对
northExit
引用的引用,以找到它到底指向什么。当
null
为空时,这样的取消引用尝试将给您带来所看到的异常

检查值是否为空的正确方法是使用参考等式(a),如下所示:

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

(a) 在教学时,我经常发现向学生借几张五美元的钞票,并对其进行解释

在参考等式方面,它们是不同的,因为它们实际上是不同的物理项。在内容或值相等方面,它们是相同的

然后我把这十块钱装进口袋,希望他们在课程结束前忘记这件事,这是我收入的一个很好的补充:-)

可能是结帐的副本,我想你会发现它很有用。如果在没有实例的情况下使用基于实例的相等方法,将得到一个NullPointerException:“null.equals(null)”没有意义,对吗?改为使用==。