I am trying to create an array of lines in a csv file.
Example csv:
12,13,14,15
13,14,15,16
11,12,13,14
Now i want my array to contain 3 strings.
I got this:
public static String[] postcodeRows;
public static void main(String[] arg) throws Exception
{
//read the csv file
BufferedReader CSVFile = new BufferedReader(new FileReader(
"C:\\Users\\randy\\Documents\\postcode csv\\exports\\all-groups.csv"));
//count where we are in the csv file
int csvLine = 0;
postcodeRows[0] = CSVFile.readLine(); // Insert the first line.
// The while checks to see if the data is null. If it is, we've hit the end of the file. If not, process the data
while (postcodeRows[csvLine] != null)
{
csvLine++;
postcodeRows[csvLine] = CSVFile.readLine();
}
// Close the file once all data has been read.
CSVFile.close();
}
Right now I get this:
Exception in thread "main" java.lang.NullPointerException
at postcodeCheckup.postcodePanel.main(postcodePanel.java:43)
Why is there a NullPointerException and how can I prevent this? The variable can’t be null, that’s checked by the while loop.
Yes, it can be null, you’re looking at the wrong thing. 🙂
The array postCodeRows is not initialized.
Check out http://mathbits.com/MathBits/Java/arrays/Initialize.htm.
Cheers.