I'm working on a code to for the game bulls and cows (http://en.wikipedia.org/wiki/Bulls_and_cows) My code compiles but when I try and run it in Terminal I get a null pointer exception error for howManyBylls, playOneTurn, and playGame. I can't find where this error originates from. Also this version of the game that differs from other games of bulls and cows in that users can input a four digit number with repeating digits. I think in most other codes they are supposed to input four digit number with unique digits.
public int howManyBulls(String guess)
{
input = guess;
bulls = 0;
for (int i = 0; i<4; i++)
{
int k = Integer.parseInt(pattern.substring(i, i+1));
int l = Integer.parseInt(input.substring(i, i+1));
if (k == l) // checking for same value at same location
{
bulls++; // add one to bull if there is a match
}
}
return bulls;
}
public class Game{
private int turns;
private Oracle computer;
private Scanner input;
public String userInput;
public int bulls;
public int cows;
public Game(){
// creates new data type Oracle
computer = new Oracle();
turns = 0;
input = new Scanner(System.in);
}
public void playGame(){
// your code for the Game playGame method goes here
System.out.print("Please enter a 4 digit number: ");
userInput=input.next();
playOneTurn();
}
// plays a turn
public void playOneTurn(){
turns++;
// passes userInput into methods of howManyBulls() and howManyCows()
bulls = computer.howManyBulls(userInput);
cows = computer.howManyCows(userInput);
System.out.println(bulls+ " bulls");
System.out.println(cows+ " cows");
// checks if game is over
if (bulls < 4) // if bulls less than four then continue playing game
// if bulls = 4 then the number is correct
{
playGame();
} else { // done with game and print out number of turns
System.out.print("It took" + turns + " turns to guess "
+ "the correct number");
}
}
public void setPattern(String solution){
computer.setPattern(solution);
}
public String getPattern(){
return computer.getPattern();
}
}