NullPointerException,即使我的变量已声明并初始化?

NullPointerException even though my variables are declared and initialized?

提问人:Johann 提问时间:7/8/2023 最后编辑:Johann 更新时间:7/9/2023 访问量:55

问:

我的代码在开始时工作,用户输入他们的代码让 AI 使用随机生成的 4 位数字进行解决,每个数字彼此唯一。

主类

            playerGuess = easy.getPlayerGuess(attempt);
            computerGuess = easy.getComputerGuess(attempt);
            
            System.out.println(playerGuess+" "+computerGuess);
            
            easy.countBulls(playerGuess, computerCode);
            easy.countCows(playerGuess, computerCode);
            easy.countCompBulls(computerGuess, playerCode);
            easy.countCompCows(computerGuess, playerCode);

我调用一个 getter 来初始化“computerGuess”的变量,该 getter 产生它的值。但是当返回计数时,它说变量为 null。

Counter 类

    public void countCompBulls(String Cguess, String UsecretCode) {
        setCompBulls(0);
        for (int i = 0; i < getCodeLength(); i++) {
            if (Cguess.charAt(i) == getUserCode().charAt(i)) {
                int ctr = 0;
                setCompBulls(ctr++);
            }
        }
    }

吸气剂

    public String getComputerGuess(int attempt) {
        if (attempt == 1){ 
            return getSecretCode();
        }
        String compGuess = generateUniqueRandomCode();
        return compGuess;
    }

Code Generator 类

    public String generateCompCode() {
        List<Integer> digits = new ArrayList<>();
        for (int i = 0; i <= 9; i++) {
            digits.add(i);
        }
        Collections.shuffle(digits);

        StringBuilder codeBuilder = new StringBuilder();
        for (int i = 0; i < CODE_LENGTH; i++) {
            codeBuilder.append(digits.get(i));
        }
        
        return codeBuilder.toString();
    }
    
    public String generateUniqueRandomCode() {
        String CompCode;
        do {
            CompCode = generateCompCode();
        } while (!isValidCode(CompCode));
        return CompCode;
    }

据我所知,这些是生成该随机数所涉及的。

我尝试将“computerGuess”放在它所在的for循环块之外,看看是否有任何变化。当我将其作为类变量放置时,也发生了同样的事情。

完整的主代码:

  public static void playGame() {
  System.out.println("Choose 1 for Easy AI, Choose 2 for Medium AI");
  int choice = in.nextInt();


  p.getPlayerCodeFromUser();
  String playerCode = p.getUserCode();
  p.setSecretCode();
  String computerCode = p.getSecretCode(); 

  String playerGuess = "";
  String computerGuess;
  for (int attempt = 1; attempt <= 7; attempt++) {
    System.out.println("\nAttempt " + attempt);
    
    if(choice == 1){
        
    playerGuess = easy.getPlayerGuess(attempt);
    computerGuess = easy.getComputerGuess(attempt);
    
    System.out.println(playerGuess+" "+computerGuess);
    
    easy.countBulls(playerGuess, computerCode);
    easy.countCows(playerGuess, computerCode);
    easy.countCompBulls(computerGuess, playerCode);
    easy.countCompCows(computerGuess, playerCode);
    }

吸气剂类:

    public String getComputerGuess(int attempt) {
    if (attempt == 1){ 
        return getSecretCode();
    }
    String compGuess = generateUniqueRandomCode();
    return compGuess;
    }

        public void countCompBulls(String Cguess, String UsecretCode) 
    {
    setCompBulls(0);
    for (int i = 0; i < getCodeLength(); i++) {
        if (Cguess.charAt(i) == getUserCode().charAt(i)) {
            int ctr = 0;
            setCompBulls(ctr++);
     }
     }
     }

    public void countCompCows(String Cguess, String UsecretCode) {
    setCompCows(0);
    for (int i = 0; i < getCodeLength(); i++) {
        if (Cguess.charAt(i) != UsecretCode.charAt(i) && 
    UsecretCode.contains(String.valueOf(Cguess.charAt(i)))) {
            int ctr = 0;
            setCompCows(ctr++);
    }
    }
    } 

示例和堆栈跟踪:

Welcome to Bulls and Cows Game!
You will be playing against the computer.
Try to guess each other's secret code.
Choose 1 for Easy AI, Choose 2 for Medium AI
1

Attempt 1
Enter your secret code (4 digits, no repeated digits): 1234
Enter your guess (attempt 1): 1234
1234 null
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.charAt(int)" because "Cguess" is null
    at Task4.EasyAi.countCompBulls(EasyAi.java:49)
    at Task4.Task4Main.playGame(Task4Main.java:46)
    at Task4.Task4Main.main(Task4Main.java:14)
Local\NetBeans\Cache\18\executor-snippets\run.xml:111: The following error occurred while executing this line:
Local\NetBeans\Cache\18\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 14 seconds)
java oop nullpointerexception

评论

1赞 Mark Rotteveel 7/8/2023
请提供最小的可重现示例和完整的异常堆栈跟踪
0赞 Johann 7/8/2023
我会编辑帖子
0赞 Mark Rotteveel 7/9/2023
您尚未提供最小的可重现示例。从打印出来可以明显看出。找出原因。computerGuessnull

答:

0赞 xScub 7/9/2023 #1

我只能假设这是因为当你调用

computerGuess = easy.getComputerGuess(attempt);

尝试是 == 1,而你,我没有看到这个密码在哪里初始化,所以我只能假设它是空的。return getSecretCode().

我会开始在那里寻找,以确保密码被正确初始化。