Michael Moore
Michael Moore

Reputation: 29

not sure why I'm getting this Null Pointer Exception error

I'm working on a text-based adventure game project. It involves rooms with items in them and navigating from room to room. There's a class called Item and this larger class called Room. All of my methods seem to work except addNeighbor (and presumably getNeighbor too, then.) I created a room with an item and that worked just fine, and I created a second room, but when I tried to add a neighbor it crashed and gave me a Null Pointer Exception. What am I doing wrong here?

public class Room
{
    private String roomDescription;
    private Item item;
    private HashMap <String, Room> myNeighbors;

    public Room (String pDescription){
        roomDescription = pDescription;
        item = null;
    }

    public Room (String pDescription, Item pItem){
        roomDescription = pDescription;
        item = pItem;
    }

    public String getRoomDescription(){
        return roomDescription;
    }

    public Item getItem(){
        return item;
    }

    public void addItem (Item i){
        item = i;
    }

    public boolean hasItem(){
        if(item != null){
            return true;
        }else{
            return false;
        }
    }

    public void addNeighbor (String pDirection, Room r){
        myNeighbors.put(pDirection, r);
    }

    public Room getNeighbor (String pDirection){
        return myNeighbors.get(pDirection);
    }

    public Item removeItem(){
        item = null;
        return item;
    }

    public String getLongDescription(){
        String longDescription = "You are at " + roomDescription + "You see " + item;
        return longDescription;
    }
}

Upvotes: 1

Views: 157

Answers (4)

mshsayem
mshsayem

Reputation: 18008

You have to new the map. Change the constructor like:

public Room (String pDescription, Item pItem)
{
        roomDescription = pDescription;
        item = pItem;
        // add this
        myNeighbors = new HashMap <String, Room>();
}

and change the other constructor to:

public Room (String pDescription)
{
        this(pDescription, null);
}

Upvotes: 1

user973999
user973999

Reputation:

my neighbors is never initialized.

You forgot this :

 private HashMap <String, Room> myNeighbors= new HashMap<String Room>();

Regards.

Upvotes: 1

Pau Kiat Wee
Pau Kiat Wee

Reputation: 9505

The following code is not null safe:

public void addNeighbor (String pDirection, Room r){
    myNeighbors.put(pDirection, r);
}

public Room getNeighbor (String pDirection){
    return myNeighbors.get(pDirection);
}

you can try:

public void addNeighbor (String pDirection, Room r){
    if(myNeighbors == null) {
       myNeighbors = new HashMap <String, Room>();
    }
    myNeighbors.put(pDirection, r);
}

public Room getNeighbor (String pDirection){
    if(myNeighbors == null) {
       myNeighbors = new HashMap <String, Room>();
    }
    return myNeighbors.get(pDirection);
}

Or

private HashMap <String, Room> myNeighbors = new HashMap <String, Room>();

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

You've never initialised myNeighbors; it's just a reference that points to nowhere.

Consider private HashMap <String, Room> myNeighbors = new HashMap<String,Room>();.

Upvotes: 2

Related Questions