1

can anyone tell me why I'm getting the error

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

This is my code. it is designed to choose a random Parea and print it to the screen and then the Earea array in the same way. (please ignore the Areabase and the switch unless that is contributing to the error. Areabase is only there temporary)

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;
		}
	}
}
Any ideas? Thank you for your help :)

2
  • learn some tutorials about the data types and arrays usage in java Commented Aug 21, 2015 at 7:48
  • 1
    String[] Parea = null; you can't fill null with elements. Initialize them properly, like String[] Parea = new String[size]; Commented Aug 21, 2015 at 7:50

1 Answer 1

3

you have to set the length for the arrays first use them in Java

change these

private static final String[] Parea = null;
private static final String[] Earea = null;

to this

private static final String[] Parea = new String[16];
private static final String[] Earea = new String[6];
Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou worked perfectly, also I didn't think I was gonna get an answer this quick :)
Happy to help :). hit stackoverflow and get the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.