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

时间:2018-12-31 20:00:13

标签: java arrays eclipse nullpointerexception

我正在做一个子手游戏。但是,当我尝试输出玩家必须猜出的单词但作为空白字符时,我很挣扎。如果单词是“ great”,那么程序将输出_ _ _ _ _

我使用了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);
    }
}

3 个答案:

答案 0 :(得分: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[])喜欢

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

尝试一下。由于您是Java新手,所以我将整个程序粘贴了修复程序。

解决方案:您所需要的只是在条件中添加以下条件。这是必需的,因为您只是声明了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);
    }
}

输出:


Hangman

很棒