Tuesday 6 August 2013

NullPointerException in Array Build from File

NullPointerException in Array Build from File

The following code builds a "2D" array from strings in a text file. At
present it is returning a NullPointException error on the line:
temp = thisLine.split(delimiter);
My question is, am I correct in understanding that temp is returning null?
If so, why, and how do I add a check for null? I'm rather new to Java, and
this is my first attempt at creating a string array of arrays from a file.
public static String[][] LibToArray()
{
String thisLine;
String[] temp;
String delimiter=",";
String [][] hexLibrary = new String[502][2];
try
{
BufferedReader br= new BufferedReader(new
FileReader("hexlibrary.txt"));
for (int j=0; j<502; j++) {
thisLine=br.readLine();
temp = thisLine.split(delimiter);
for (int i = 0; i < 2; i++) {
hexLibrary[j][i]=temp[i];
}
}
}
catch (IOException ex) { // E.H. for try
JOptionPane.showMessageDialog(null, "File not found. Check name
and directory."); // error message
}
return hexLibrary;
}

No comments:

Post a Comment