I am getting a null pointer exception on my nameA = view.getHumanName(nameB);
line in my model class. I am trying to establish nameA
using the getHumanName
method from the view class. I also attempted to add in this.view = view
to the model constructor but that does not fix the issue. Why am I getting this error in this instance?
Model class and instance variable:
public Model(Dice die){
nameB = "Toby";
nameA = view.getHumanName(nameB);
p1 = new Player(nameA);
p2 = new Player(nameB);
this.die = die;
}
}
Method in View class:
public String getHumanName(String compName){
System.out.println("Enter a name for the human player: ");
String humanName = scan.nextLine();
while(humanName.equals(compName) || humanName.trim().isEmpty()){
System.out.println("The human player cannot have the same name as the computer player or be blank. Enter a new name for the human player: ");
humanName = scan.nextLine();
}
return humanName;
}