This question already has an answer here:
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));
}
}
Aucun commentaire:
Enregistrer un commentaire