This question already has answers here:
What is a NullPointerException, and how do I fix it?

(12个答案)


5年前关闭。




我想通过让所有人都知道这是我第一次出现堆栈溢出来提出这个问题,所以如果我不符合提问标准,请告诉我。

我正在制作一个与您一起玩摇滚,剪纸,剪刀的程序,正当我接近项目的后端时,出现了此错误:
Exception in thread "main" java.lang.NullPointerException
  at RockPaperScissors.getPlayerThrow(RockPaperScissors.java:93)
  at RockPaperScissors.main(RockPaperScissors.java:26)

我不确定我将在哪里使用null,但这就是你在这里的目的。

这是当前编译的整个项目:
public class RockPaperScissors {
//sets the constants
static final int ROCK = 1;
static final int PAPER = 2;
static final int SCISSORS = 3;

//creates some variables
static int playerThrow, computerThrow, result, timesPlayed, playerWins, computerWins;
static String playAgain;
static Scanner fru;

/*
 * The Results
 * 0 = tie
 * 1 = Player win
 * 2 = Computer win
*/

public static void main(String[] args) {
    //this do while loop is the whole game
    do {
        //decides the throws of the players
        playerThrow = getPlayerThrow();
        computerThrow = (int)(Math.random() * 3 + 1);

        switch(playerThrow) {
        //compares and displays the computer and player
        //choices if the player chooses rock
        case ROCK:
            switch(computerThrow) {
            case ROCK:
                result = 0;
                System.out.println("You threw rock and the computer threw rock!");
                break;
            case PAPER:
                result = 2;
                System.out.println("You threw rock and the computer threw paper!");
                break;
            case SCISSORS:
                result = 1;
                System.out.println("You threw rock and the computer threw scissors!");
                break;
            }   break;
        //compares and displays the computer and player
        //choices if the player throws paper
        case PAPER:
            switch(computerThrow) {
            case ROCK:
                result = 1;
                System.out.println("You threw paper and the computer threw rock!");
                break;
            case PAPER:
                result = 2;
                System.out.println("You threw paper and the computer threw paper!");
                break;
            case SCISSORS:
                result = 3;
                System.out.println("You threw paper and the computer threw scissors!");
                break;
            }   break;
        //compares and displays the computer and player
        //choices if the player throws scissors
        case SCISSORS:
            switch(computerThrow) {
            case ROCK:
                result = 2;
                System.out.println("You threw scissors and the computer threw rock!");
                break;
            case PAPER:
                result = 1;
                System.out.println("You threw scissors and the computer threw paper!");
                break;
            case SCISSORS:
                result = 0;
                System.out.print("You threw scissors and the computer threw scissors!");
                break;
            }   break;
        }
        timesPlayed ++;

        // will compare and decide the winner of the two players
        finish();

    } while (timesPlayed < 3);
}

public static int getPlayerThrow() {
    //prompts weapon choice and stores said choice
    System.out.println("Choose your weapon of choice:\n(1 for rock, 2 for paper, 3 for scissors)");
    int choice = fru.nextInt();

    //checks for validity and returns the choice
    if (choice != 1 && choice != 2 && choice != 3) {
        System.out.print("Not a valid input!\n Please try again: ");
        choice = fru.nextInt();
    }

    return choice;
}

//compares and decides the winner of the two players
public static void finish() {
    //displays the winner of the round accourding to aforementioned possible results
    switch(result) {
    case 0:
        System.out.println("Its a tie!"); break;
    case 1:
        System.out.println("You are victorious! Man over machine!");
         playerWins++; break;
    case 2:
        System.out.println("The computer has taken the round! Technological singularity approaches!");
        computerWins++; break;
    }

    //cheks if the match is over and displays messages accordingly
    switch(timesPlayed) {
    case 1: break;
    case 2:
        if (playerWins == 2 || computerWins == 2) {
            if (playerWins == 2) {
                System.out.println("You win the match! Congratulations!\nWould you like to play another match?\n(y for yes, n for no)");
                timesPlayed = 5;
                playAgain = fru.nextLine();

                //checks for validity
                if (playAgain != "y" || playAgain != "n") {
                    System.out.print("Not a valid input!\n Please try again: ");
                    playAgain = fru.nextLine();
                }
            }
            else if (computerWins == 2) {
                System.out.println("The computer wins the match!\nPlay again! I know you can beat it.\n(y for yes, n for no)");
                timesPlayed = 5;
                playAgain = fru.nextLine();

                //checks for validity
                if (playAgain != "y" || playAgain != "n") {
                    System.out.print("Not a valid input!\n Please try again: ");
                    playAgain = fru.nextLine();
                }
            }
        } break;
    //will happen for any amount of times played over 2
    default:
        if (playerWins == 2) {
            System.out.println("You win the match! Congratulations!\nWould you like to play another match?\n(y for yes, n for no)");
            playAgain = fru.nextLine();

            //checks for validity
            if (playAgain != "y" || playAgain != "n") {
                System.out.print("Not a valid input!\n Please try again: ");
                playAgain = fru.nextLine();
            }
        }
        else if (computerWins == 2) {
            System.out.println("The computer wins the match!\nPlay again! I know you can beat it.\n(y for yes, n for no)");
            playAgain = fru.nextLine();

            //checks for validity
            if (playAgain != "y" || playAgain != "n") {
                System.out.print("Not a valid input!\n Please try again: ");
                playAgain = fru.nextLine();
            }
        }
    }
}
}

我既不了解错误的含义,也不了解错误的来源。我仅有的有关该错误的信息来自于Google略读错误的信息,但是当所问的问题或示例不特定于我的项目时,这将非常困难。我已经采取了多个步骤来修复它,但似乎没有任何作用。

这可能是一个复杂的编码问题,也可能是我遗漏的单个字符,但是我们不胜感激!谢谢!

最佳答案

我看到很多地方都可以使用fru扫描仪,但实际上没有地方可以初始化它。您所拥有的基本上可以归结为:

import java.util.Scanner;
public class Test {
    static Scanner fru;
    public static void main(String[] arg) {
        int x = fru.nextInt();
        System.out.println(x+1);
    }
}

并且,当您运行该命令时,您将看到异常。您需要执行以下操作:
fru = new Scanner(System.in);

在尝试进行任何扫描之前。在执行此操作之前,尝试取消引用它会导致您遇到异常。换句话说,类似:
import java.util.Scanner;
public class Test {
    static Scanner fru;
    public static void main(String[] arg) {
        fru = new Scanner(System.in);
        int x = fru.nextInt();
        System.out.println(x+1);
    }
}

可以成功运行。

就将来发现这些问题而言,最好查看一下实际收到的错误消息:
Exception in thread "main" java.lang.NullPointerException
  at RockPaperScissors.getPlayerThrow(RockPaperScissors.java:93)
  at RockPaperScissors.main(RockPaperScissors.java:26)

尽管事实上您的错误消息与您的代码不完全匹配(行号减少了三位),但检查堆栈跟踪以找出错误的位置和原因是至关重要的。

在这种情况下,该行是:
int choice = fru.nextInt();

因此您会认为这是因为fru设置不正确。从那里开始,就可以追溯到将fru实际设置为有用的地方。在这种特殊情况下,它不存在,因此相对容易找出。

10-08 01:47