随机化数组变量时出现线程“主”错误的异常

时间:2015-08-21 07:43:41

标签: java arrays random

任何人都可以告诉我为什么我会收到错误

Exception in thread "main" java.lang.NullPointerException
at test_package.First_class.main(First_class.java:14)

这是我的代码。它被设计为选择随机Parea并以相同的方式将其打印到屏幕然后再打印到E​​area阵列。 (请忽略Areabase和交换机,除非导致错误.Areabase只是暂时的)

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;
		}
	}
}
有任何想法吗? 谢谢你的帮助:)

1 个答案:

答案 0 :(得分:2)

你必须设置数组的长度首先在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];