谁能告诉我为什么会收到错误
Exception in thread "main" java.lang.NullPointerException
at test_package.First_class.main(First_class.java:14)
这是我的代码。它的设计目的是选择一个随机 Parea 并将其打印到屏幕上,然后以相同的方式打印到 Earea 数组。 (请忽略区域数据库和开关,除非这会导致错误。区域数据库只是暂时的)
package test_package;
import java.util.Scanner;
public class First_class {
private static final String[] Parea = null;
private static final String[] Earea = null;
public static void main(String[] args){
int Areabase = 1;
Scanner keyin = new Scanner(System.in);
switch (Areabase) {
case 1:
Parea[1] = "You enter what looks like it was once the ship cafateria but is now in ruin.";
Parea[2] = "You find yourself in a hallway, some of the lights are broken and the doors barely work.";
Parea[3] = "You enter an old storage room and almost slip into the huge hole that has opened up in the floor.";
Parea[4] = "You walk into the ships bathroom, the toilets are overflowing and the soap has a bite out of it.";
Parea[5] = "This seems to be an outdated controll deck, you wonder why it is here then you decide to move on.";
Parea[6] = "You emerge into the libary and almost trip over a stack of books. i lybrarian woould scream at this mess";
Parea[7] = "You find yourself in a room you dont reconise. there is glass all over the ground.";
Parea[8] = "You enter the main controll room. it seems that the entire front of the ship has colapsed.";
Parea[9] = "You walk into the ship's dorm. one room is littered with cat posters. Hang in there one says.";
Parea[10] = "This seems to have once been a black marketing room. you think you now understand the bounty.";
Parea[11] = "You emerge into a rapidly changing simulation room. it changes to a beach, a castle, YOUR SHIP?";
Parea[12] = "You move into the prisoner room. all the cells are open. people must have busted out.";
Parea[13] = "You trip and fall into the life support room. this room is badly dammage, badly badly dammages.";
Parea[14] = "You float into a random empty room. you emediatly know what it is. an anti gravity room.";
Parea[15] = "This room appears to be a half caved in engenerring room. maby you should leave befor those barrels explode.";
Earea[1] = "This room is on fire";
Earea[2] = "An explotion rocks the ship";
Earea[3] = "This room smells foul";
Earea[4] = "you wish you could take this room back to your ship";
Earea[5] = "THIS ROOM IS FILLED WITH ANIME";
int random = (int) Math.floor(Math.random() * (15 - 1) + 1);
int random2 = (int) Math.floor(Math.random() * (5 - 1) + 1);
System.out.println(Parea[random]);
System.out.println(Parea[random2]);
break;
default:
break;
}
}
}
最佳答案
您必须先设置数组的长度才能在 Java 中使用它们
更改这些
private static final String[] Parea = null;
private static final String[] Earea = null;
到此
private static final String[] Parea = new String[16];
private static final String[] Earea = new String[6];
关于java - 随机化数组变量时线程 "main"错误中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32134937/