【问题标题】:How To Fix Java.lang.NullPointerException When Creating Arrays [duplicate]创建数组时如何修复 Java.lang.NullPointerException [重复]
【发布时间】:2018-12-31 20:00:12
【问题描述】:

我正在制作一个刽子手游戏。然而,当我试图输出玩家必须猜测的单词但作为空白字符时,我很挣扎。如果这个词是“伟大的”,程序会输出 _ _ _ _ _

我使用 for 循环创建了一个与单词具有相同数量字符的数组,但是每当我运行我的代码时,我都会得到一个 java.lang.NullPointerException

我已尝试研究该问题并查看 java 文档,但是我是该语言的新手,发现很难理解该问题以及如何解决它。我哪里错了?

import java.util.*;

public class Hangman 
{
    static char[] word1 = {'a', 'm', 'a', 'z', 'i', 'n', 'g'};
    static char[] word2 = {'f', 'a', 'b', 'u', 'l', 'o', 'u', 's'};
    static char[] word3 = {'g', 'r', 'e', 'a', 't'};
    static Random random = new Random();
    static int choice;
    static char[] choice_array;
    static char[] num_of_spaces;

    public static void main(String args[])
    {
        System.out.println("-------");
        System.out.println("Hangman");
        System.out.println("-------");
        choice = random.nextInt(3)+1;
        switch (choice)
        {
        case 1:
            choice_array = word1;
            break;
        case 2:
            choice_array = word2;
            break;
        case 3:
            choice_array = word3;
            break;
        }
        System.out.println(choice_array);
        for(int counter = 0; counter < choice_array.length; counter++)
        {
            num_of_spaces[counter] = '_';
        }
        System.out.println(num_of_spaces);
    }
}

【问题讨论】:

  • 我们能否获得完整的堆栈跟踪(抛出的错误)?
  • Exception in thread "main" java.lang.NullPointerException at Hangman.main(Hangman.java:33)
  • 你永远不会初始化num_of_spaces
  • 你在哪里初始化了这个数组static char[] num_of_spaces;

标签: java arrays eclipse nullpointerexception


【解决方案1】:

您从未初始化过num_of_spaces。在循环之前这样做。喜欢,

System.out.println(choice_array);
num_of_spaces = new char[choice_array.length];
for(int counter = 0; counter < choice_array.length; counter++)
{
    num_of_spaces[counter] = '_';
}

 num_of_spaces = new char[choice_array.length];
 Arrays.fill(num_of_spaces, '_');

并打印你想要Arrays.toString(char[]) like的数组

System.out.println(Arrays.toString(num_of_spaces));

System.out.println(new String(num_of_spaces));

【讨论】:

  • 感谢 Eliott 的帮助,我不知道在使用它之前我必须初始化数组。
  • @SHatna 你可能也考虑static char[] word1 = "amazing".toCharArray();
【解决方案2】:

数组num_of_spaces 从未初始化。因此,在使用[counter] 为其赋值时,它没有分配空间,也不知道它可以存储多少项目。

试试num_of_spaces = new char[choice_array.length];。那应该可以解决。

【讨论】:

    【解决方案3】:

    试试这个。由于您是 Java 新手,因此我已将整个程序与修复一起粘贴。

    解决方案:您只需在 for 循环中添加以下 if 条件即可。这是必需的,因为您刚刚声明了 num_of_spaces 变量但未初始化。如果您没有初始化变量并尝试访问它,java 总是返回 NullpointerException。

    if(num_of_spaces==null){
        num_of_spaces = new char[choice_array.length];
    }
    

    修复后,您的程序应如下所示。

    import java.util.Random;
    
    public class Hangman
    {
        static char[] word1 = {'a', 'm', 'a', 'z', 'i', 'n', 'g'};
        static char[] word2 = {'f', 'a', 'b', 'u', 'l', 'o', 'u', 's'};
        static char[] word3 = {'g', 'r', 'e', 'a', 't'};
        static Random random = new Random();
        static int choice;
        static char[] choice_array;
        static char[] num_of_spaces;
    
        public static void main(String args[])
        {
            System.out.println("-------");
            System.out.println("Hangman");
            System.out.println("-------");
            choice = random.nextInt(3)+1;
            switch (choice)
            {
                case 1:
                    choice_array = word1;
                    break;
                case 2:
                    choice_array = word2;
                    break;
                case 3:
                    choice_array = word3;
                    break;
            }
            System.out.println(choice_array);
            for(int counter = 0; counter < choice_array.length; counter++)
            {
                if(num_of_spaces==null){
                    num_of_spaces = new char[choice_array.length];
                }
                num_of_spaces[counter] = '_';
            }
            System.out.println(num_of_spaces);
        }
    }
    

    输出:


    刽子手

    很好


    【讨论】:

      猜你喜欢
      • 2016-06-19
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多