简体   繁体   English

Bulls and Cows游戏程序Java。 运行程序时出现空指针异常错误

[英]Bulls and Cows game program Java. Null pointer exception error when running the program

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. 我正在为牛和牛游戏编写代码( http://en.wikipedia.org/wiki/Bulls_and_cows )我的代码可以编译,但是当我尝试在终端中运行它时,howManyBylls出现空指针异常错误,playOneTurn和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();
    }
}

It appears the variable "pattern" that appears in howManyBulls() has not been initialized or it has been set to null. 似乎出现在howManyBulls()中的变量“ pattern”尚未初始化或已设置为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. 空指针异常通常是由在空对象上调用方法引起的,如果尚未初始化变量,则可能发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM