0

I wanna test the method DrawCard() from the class ModelPlayer that takes the first not-null Color value from the arrayList PoliticCards of the class PoliticCard and puts it in the arrayList politicCards of the class Player; and i get two NullPointerExceptions.

public enum Color {
    BLACK, PURPLE
}
public class PoliticCard {
    private static ArrayList<Color> politicCards;
    public PoliticCard(){
        setPoliticCards(new ArrayList<Color>());
        int i=0;
        while (i<13){//adds 13 Color values set to BLACK
            politicCards.add(Color.BLACK);
            i++;
        }
        while (i<26){//adds 13 Color values set to PURPLE
            politicCards.add(Color.PURPLE);
            i++;
        }
        }
        Collections.shuffle(politicCards);

    }
    public static ArrayList<Color> getPoliticCards() {
        return politicCards;
    }
    public static void setPoliticCards(ArrayList<Color> politicCards) {
        PoliticCard.politicCards = politicCards;
    }


}
public class Player {
    private int id;//id of the player, goes from 1 to 10
    private ArrayList<Color> politicCards;
    public Player(int id){
        this.setId(id);
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public ArrayList<Color> getPoliticCards() {
        return politicCards;
    }
    public void setPoliticCards(ArrayList<Color> politicCards) {
        this.politicCards = politicCards;
    }
public class ModelPlayer {

    public void DrawPoliticCard(Player player){
        int i=0;
        if(PoliticCard.getPoliticCards().get(i)!=null){
                player.getPoliticCards().add(PoliticCard.getPoliticCards().get(i));//This
// is the line where i get the first NullPointerException
            PoliticCard.getPoliticCards().remove(i);
        }else{
            i++;
            player.getPoliticCards().add(PoliticCard.getPoliticCards().get(i));
            PoliticCard.getPoliticCards().remove(i);
        }

        }



}
public class ModelPlayerTest {

    @Test
    public void testDrawCard() {
        Player player = new Player(1);
        ModelPlayer modelPlayer = new ModelPlayer();
        player.setPoliticCards(null);
        new PoliticCard();
        modelPlayer.DrawPoliticCard(player);//This is the second line where i get NullPointerException
        assertNotNull(player.getPoliticCards().get(0));
    }

}
  • Hmm... Could it be `player.setPoliticCards(null);` later followed by `player.getPoliticCards()`?? – OneCricketeer May 28 '16 at 16:14
  • You can also add black and purple cards into the list in a single loop. You shuffle anyways, so the order doesn't matter – OneCricketeer May 28 '16 at 16:16
  • but isn't the line `modelPlayer.DrawPoliticCard(player); ` adding values to the empty arrayList? – user2250393 May 28 '16 at 16:17
  • The players arraylist is null... Because you explicitly set it so. The arraylists are not shared. The player has a different `politicCards` than the `PoliticCards.politicCards` – OneCricketeer May 28 '16 at 16:19
  • 1
    Did you step through with a debugger using breakpoints? – Ascalonian May 28 '16 at 16:22
  • For a typical card game, you can use methods like `Deck.getCards()`, `Player.drawFrom(Deck)` and `Player.getHand()`. A Deck need not contain a static list of cards (meaning all instances of a deck have the exact same set of cards) – OneCricketeer May 28 '16 at 16:22

0 Answers0