I am having an issue with a String array and trying to add it to a list (List).
Below is the code that is used and creating the issue.
The program fails on the first run through of the loop and I have verified the input which comes from a CSV using OpenCSV.
List<String[]> output = null;
String[] temp;
for(int i = 0; i < 13; i++)
{
temp = reader.readNext(); //read next line into temp
System.out.println(temp[0]+temp[1]+temp[2]); //temp output
temp[2] = String.valueOf((values[i])/100); //assign new value
System.out.println(temp[0]+temp[1]+temp[2]); //temp output
output.add(temp);
}
When this code runs the output is.
VANCBULLET0.311
VANCBULLET0.308
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Main.updateCSV(Main.java:951)
at Main.start(Main.java:863)
at Main.access$23(Main.java:853)
at Main$23.actionPerformed(Main.java:520)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
The first two lines are correct and are split up like:
temp[0] temp[1] temp[2]
VANC BULLET 0.311
VANC BULLET 0.308
The issue is (like the error reads) at:
output.add(temp);
The documentation reads:
NullPointerException - if the specified element is null and this list does not permit null elements
but as you can see from my output (the second line) the array “temp” is not null, it contains “VANC BULLET 0.308” in each element respectively.
I’m stumped. Does anyone have an idea or see something I have failed to see?
Thanks
From what i can see you never initialized
List<String[]> output = null;in your code. Thus when it call’sList.addas List is still null it throws NPEinitialize it first: