代码之家  ›  专栏  ›  技术社区  ›  SHatna

创建数组时如何修复Java.lang.NullPointerException[重复]

  •  -2
  • SHatna  · 技术社区  · 7 年前

    我在做一个刽子手游戏。但是,当我试图输出玩家必须猜测的单词时,我感到很困难,但输出为空白字符,例如,如果单词是“伟大的”,程序将输出_

    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);
        }
    }
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Elliott Frisch    7 年前

    你从未初始化过 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[]) 喜欢

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

    或

    System.out.println(new String(num_of_spaces));
    
        2
  •  1
  •   Raman Shrivastava    7 年前

    阵列 num_of_spaces 从未初始化。因此,在使用 [counter] 它没有分配空间,不知道可以存储多少项。

    尝试 num_of_spaces = new char[choice_array.length]; . 这应该可以解决问题。

        3
  •  0
  •   NNK    7 年前

    试试这个。因为您是java新手,所以我用fix粘贴了整个程序。

    解决方案:您只需将以下if条件添加到for循环中。这是必需的,因为您刚刚声明了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);
        }
    }
    

    输出:


    刽子手

    伟大的