0

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();
    }
}
wics13
  • 9
  • 1
  • 1
    Please include the complete stack trace you get when running your program. – Martin Oct 28 '14 at 16:39
  • 2
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Rick S Oct 28 '14 at 16:40
  • Please enter a 4 digit number: 1223 Exception in thread "main" java.lang.NullPointerException at Oracle.howManyBulls(Oracle.java:68) at Game.playOneTurn(Game.java:38) at Game.playGame(Game.java:30) at BullsAndCows.main(BullsAndCows.java:11) – wics13 Oct 28 '14 at 16:43
  • 1
    So what is the line 68? I guess `pattern` is null. – Nikolay K Oct 28 '14 at 16:47

1 Answers1

0

It appears the variable "pattern" that appears in howManyBulls() has not been initialized or it has been set to null. It is not defined in the code you have posted and is the only variable that has not been initialized. A null pointer exception is generally caused by calling a method on a null object, which can happen if a variable has not been initialized.

Frank
  • 9
  • 2
  • I created a separate method that defines the string pattern as four randomly generated digits concatenated into a string. – wics13 Oct 28 '14 at 16:57
  • @wics13 And are you calling that method? In other words, is the `pattern` variable getting setup before this method is called? (Also hint, set a breakpoint in the `howManyBulls` method and see which variable specifically is null, then trace it back from there.) – Paul Richter Oct 28 '14 at 17:07