0

So I have been viewing stack overflow to see if any questions have been answered referring to my problem, but I feel as if my array was indeed initialized. Currently I am attempting to make a simple card game but I continue to get the same error no matter what I do.

Main:

public class Main {
public static void main(String[] args) {
    Dealer deal = new Dealer();
    User use = new User();
    Player play = null;
    Logistics log = new Logistics();

    boolean[] initElementList = {false, false, false};
    use.initElementList(initElementList);
    deal.initElementList(initElementList);

    while(play == null){
        use.printDeck();
        Card userChoice = use.chooseCard();
        Card dealerChoice = deal.chooseCard();
        **log.roundWinner(userChoice, dealerChoice);**
        play = log.gameWinner();
    }
    System.out.println(play);
}
}

Logistics:

public Card roundWinner(Card user, Card dealer){
    Dealer deal = new Dealer();
    User use = new User();
    String userChoiceE = user.getElement();
    String dealerChoiceE = dealer.getElement();

    int userChoiceS = user.getStrength();
    int dealerChoiceS = dealer.getStrength();

    **if(userChoiceE.equalsIgnoreCase("FIRE") && dealerChoiceE.equalsIgnoreCase("ICE")){
        use.setElementList(0);
        return user;
    }**

Player:

public abstract class Player {

private boolean[] hasElement;
private Card[][] deck;

public boolean[] getElementList(){
    return this.hasElement;
}
**public void setElementList(int index){
    this.hasElement[index] = true;
} 
public void initElementList(boolean[] ElementList){
    this.hasElement = ElementList;
}**
public void createDeck(){
    CardGenerator cg = new CardGenerator();
    this.deck =  cg.Deck();
}
public Card[][] getDeck(){
    return this.deck;
}
}
K Newman
  • 56
  • 5
  • The error I would get is: Exception in thread "main" java.lang.NullPointerException at CardJitsu.Player.setElementList(Player.java:12) at CardJitsu.Logistics.roundWinner(Logistics.java:15) at CardJitsu.Main.main(Main.java:18) – K Newman Jan 15 '17 at 06:17
  • As well as the where you are getting the. – jack jay Jan 15 '17 at 06:19
  • The problem is you're constructing a new `User` in `roundWinner()` without initializing `hasElement`. – shmosel Jan 15 '17 at 06:21
  • @shmosel would I fix this by changing the hasElement methods to static? – K Newman Jan 15 '17 at 06:27
  • It might not throw an error, but it's probably not the right solution. You need to determine whether it's correct to be creating a new `User`, and if it is, make sure it's initialized properly. – shmosel Jan 15 '17 at 06:28

0 Answers0