简体   繁体   中英

How do i resolve this null pointer exception error (Java)?

I am creating a project on bluej based on a gym system. There is one parent class in the project which is named "Card" the child classes to this parent class include Loyalty, Standard and staff, which are all types of cards the gym system can hold.The System also has a number of Zones eg pool, sauna etc.There is also a sitemanager class which holds all the gyms information such as how many doors there are and the details of the zones.

While running an IO class named centreIO, while attempting to run this part of the code;

 private void addCard()
    {
        System.out.println("Enter card id:");
        int id = reader.nextInt();
        Card cr = hatfield.getCard(id);
        hatfield.addCard(cr);


        }

I came across a problem which pointed towards a method in the zones class. This was the method that the null exception pointed to

public void addCardtoZone(Card C)

{

    cards.put(C.getId(),C);

}

The site manager has two Hashmaps with Zones and Doors, and Zones has a hashmap of all cards currently within the Zone, so it will hold methods such as stating how many cards are currently in the zone etc. How do I fix this code so that it actually adds the card to the the site? I dont understand why this is not working because when I run the method from within the Zone class it works perfectly.

Here is the method im calling on in the IO class

public void addCard(Card cd)
{

    Outside.addCardtoZone(cd);

}

EDIT: the put methods puts a key and an object into the hashmap

private HashMap cards = new HashMap<Integer,Card>();

And here is a sample of the card class( a superclass to 3 other classes, Loyalty, standard, staff.)

public class Card
{
public String name;

public int rating;
public int credits;
public String centre;
public int type;

//Public constructor
public Card(String nam, int rat, int cred)
{

    name = nam;
    rating = rat;
    credits = cred;


}

public int getId() //getId() has been overridden in the child classes
{

    return 0;

}

public String getName()
{
    return name;
}  

/** returns member's rating
 * @return member's rating
 */
public int getRating()
{
    return rating;
}

/** changes a member's rating
 * @param rat new rating 
 */
public void changeRating(int rat)
{
    rating = rat;
}



}

}

public void addCardToZone(Card C) {
    if(cards == null) {
        cards = new HashMap<Integer, Card>();
    }
    cards.put(C.getId(),C);
}

Now your addCardToZone method will check cards for null . If it's null , it will initialize it. After this, you just put a card in as normal. The if should only enter on the first call to addCardToZone .

Rather than telling you what the solution is (which is a bit difficult since we can't see all of the source code that could be relevant ...), here's how to diagnose this.

1) Use the stacktrace to identify the line of code that throws the NPE.

You've identified it as:

  cards.put(C.getId(),C);

2) Figure out what could cause an NPE.

In this case, there are only two possible causes:

  • If cards is null then cards.put will throw an NPE.
  • If c is null then c.getId() will throw an NPE.

3) Eliminate the cases that are logically impossible ... in the context.

For example, if cards was declared as final and you had initialized it to a non-null value then you could eliminate that possible cause. Or if in a previous statement you can used the c variable in a way that would have failed if it was null, you could eliminate that.

4) Work backwards through the code repeating 3) ...

5) If that fails, either use a debugger or judicious logging / trace-printing to eliminate possible causes or confirm suspicions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM