Reputation: 3
I'm attempting to read a text file using BufferedReader
and sort the information within the file. The purpose is to load and be able to access information on maps for a game I'm making. The problem I have is that when my program tried to sort the information I get a NullPointerException
.
LoadWorld.java:
public class LoadWorld {
private static String CurrentString;
private static String[] LinePts;
private static String Path="";
private static String Name="";
private static String Auth="";
private static String Date="";
private static boolean Reading = true;
private static boolean OnMap = false;
private static int Maps = 0;
private static Map[] Map;
public static void Load() {
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(LoadWorld.class.getResourceAsStream("/Maps/Maps.txt")));
while(Reading) {
CurrentString = reader.readLine();
if (CurrentString.equals("{")) {OnMap=true;Maps+=1;}
else {Reading=false;}
while(OnMap) {
CurrentString = reader.readLine();
if (!(CurrentString.equals("}"))) {
LinePts = CurrentString.split("-");
if (LinePts[0].equals("PATH")) {Path=LinePts[1];}
else if (LinePts[0].equals("NAME")) {Name=LinePts[1];}
else if (LinePts[0].equals("AUTH")) {Auth=LinePts[1];}
else if (LinePts[0].equals("DATE")) {Date=LinePts[1];}
}
else {
Map[Maps].Path = Path;
Map[Maps].Name = Name;
Map[Maps].Auth = Auth;
Map[Maps].Date = Date;
OnMap=false;
}
}
}
reader.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
The problem occurs in the "else{" section, at "map[maps].path = path;".
Map.java:
public class Map {
public String Path;
public String Name;
public String Auth;
public String Date;
public String Map;
}
The text file I'm attempting to read: Maps.txt
{
PATH-Default/Basic.txt
NAME-Basic Map
AUTH-Aelex Esrom
DATE-11/1/15
}
Any help is appreciated! Thanks in advance for any answers.
Upvotes: 0
Views: 34
Reputation: 798
you need to initialize your variable Map
Map = new Map[5]; //array of fixed size 5
perhaps you are trying to dynamically add array elements the PHP way but it does not work the same way in Java. You will either create an array of a fixed size or use ArrayList
Upvotes: 1