简体   繁体   中英

Trouble with NullPointerException

I am working on a piece of code where I am working with a deck of cards. Each card in the deck is a Card class that have a rank and suit, both of which are enumerated types. I have a Deck class which holds a list of Card objects. In this deck class I have a method to add a card to the top of the deck (the first available index).

My deck is initialized like this.

}
/** A new deck is initially empty, but has the capacity to hold
 * all the cards in a standard deck. */
public Deck() {
    Card[] cards = new Card[52];
}

Then I have my method to add a card as follows.

 /** Adds card to the top of this deck. */
public void add(Card card) {
    for(int i = 0; i < 52; i++){
        if(cards[i].equals(card)){
            cards[i] = card;
            break;
        }
    }       
}

I get a NullPointerException on the line

if(cards[i].equals(card)){

The idea was to check each index in the list to find the first non-null index, but have been confronted with that NullPointerException. How can I make this run so that I find the first available index in my list of Card objects?

First perform a Null Check then proceed further :

if(cards[i]!=null && cards[i].equals(card)){
            cards[i] = card;
            break;
        }

Another problem which you might face is

cards[i] not found, since the scope of cards is only upto Constructor of Deck

So Instead try this

Card[] cards;
public Deck() {
    cards = new Card[52];
}

As neeraj said it would eliminate the exception and give ua smooth run but the problem appears to be the parameter of type card. Check whether you have initialized it to null and failed to assign it before invoking the method :)

You have just created an array in the constructor, that contains 52 references to objects of class Card. However, you haven't initialized/created any of those Card objects, so all of those references are null. That's why you are getting NullPOinterException when you try to invoke a method on the null object. You should initialize all card objects like this:

public Deck() {
    Card[] cards = new Card[52];
    for(int i=0; i<52; i++) {
        cards[i] = new Card(); //use your constructor with suit and rank
    }
}

Also - if you feel that any object (like Card here) can be null at runtime, you should handle that null scenario properly, like check that it is not null before invoking a method on that object (as others have pointed out).

Well answers from Neeraj Jain and V_Singh are correct. But keep in mind that if you do the following

...
if(cards[i].equals(card)){
                cards[i] = card;

then you will check wether this card is already in this deck. If your having like 52 cards (each card twice) you should take care that each card has its own instance. Otherwise it will just reassign that existing card.

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