创建数组时如何修复Java.lang.NullPointerException
我在做一个刽子手游戏。但是,当我试图输出玩家必须猜测的单词时,我感到很困难,但输出为空白字符,例如,如果单词是“伟大的”,程序将输出_ 我使用for循环创建了一个数组,该数组的字符数与单词的字符数相同,但每当我运行代码时,都会得到一个创建数组时如何修复Java.lang.NullPointerException,java,arrays,eclipse,nullpointerexception,Java,Arrays,Eclipse,Nullpointerexception,我在做一个刽子手游戏。但是,当我试图输出玩家必须猜测的单词时,我感到很困难,但输出为空白字符,例如,如果单词是“伟大的”,程序将输出_ 我使用for循环创建了一个数组,该数组的字符数与单词的字符数相同,但每当我运行代码时,都会得到一个java.lang.NullPointerException 我曾试图研究这个问题并查看java文档,但是我对该语言还不熟悉,发现很难理解这个问题以及如何解决它。我哪里做错了 import java.util.*; public class Hangman {
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);
}
}
import java.util.*;
公共级刽子手
{
静态字符[]word1={'a','m','a','z','i','n','g'};
静态字符[]word2={f',a',b',u',l',o',u',s'};
静态字符[]word3={'g','r','e','a','t'};
静态随机=新随机();
静态整数选择;
静态字符[]选择_数组;
静态字符[]数量\u的\u空间;
公共静态void main(字符串参数[])
{
System.out.println(“----”);
系统输出打印(“刽子手”);
System.out.println(“----”);
选择=随机。nextInt(3)+1;
开关(选择)
{
案例1:
choice_array=word1;
打破
案例2:
choice_数组=word2;
打破
案例3:
选择_数组=word3;
打破
}
System.out.println(选择_数组);
for(int counter=0;counter
您从未初始化过num\u空间
。在循环之前这样做。像
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] = '_';
}
要打印数组,您需要Arrays.toString(char[])
like
System.out.println(Arrays.toString(num_of_spaces));
或
num_of_spaces = new char[choice_array.length];
Arrays.fill(num_of_spaces, '_');
System.out.println(new String(num_of_spaces));
数组
num\u of_spaces
从未初始化。因此,在使用[counter]
为其赋值时,它没有分配空间,也不知道可以存储多少项
尝试
num_of_spaces=newchar[choice_array.length]代码>。这应该可以解决问题。试试这个。因为您是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);
}
}
import java.util.Random;
公共级刽子手
{
静态字符[]word1={'a','m','a','z','i','n','g'};
静态字符[]word2={f',a',b',u',l',o',u',s'};
静态字符[]word3={'g','r','e','a','t'};
静态随机=新随机();
静态整数选择;
静态字符[]选择_数组;
静态字符[]数量\u的\u空间;
公共静态void main(字符串参数[])
{
System.out.println(“----”);
系统输出打印(“刽子手”);
System.out.println(“----”);
选择=随机。nextInt(3)+1;
开关(选择)
{
案例1:
choice_array=word1;
打破
案例2:
choice_数组=word2;
打破
案例3:
选择_数组=word3;
打破
}
System.out.println(选择_数组);
for(int counter=0;counter
输出:
刽子手
伟大的
我们可以拥有完整的stacktrace(抛出的错误)吗?线程“main”java.lang.NullPointerException中的异常在Hangman.main(Hangman.java:33)
你从来没有初始化过num\u of_spaces
你在哪里初始化过这个数组静态字符[]num\u of_of_spaces代码>?谢谢Eliott的帮助,我不知道在使用它之前我必须初始化数组。@ SHatna,你也可以考虑<代码>静态ch[[Word1] =“惊人”)。代码>