奇数Java字符串[]问题,包含值,但在插入列表时,其行为类似于null<;字符串[]>;
我对字符串数组有问题,并试图将其添加到列表中。 下面是用于创建问题的代码 程序在循环的第一次运行中失败,我已经使用OpenCSV验证了来自CSV的输入奇数Java字符串[]问题,包含值,但在插入列表时,其行为类似于null<;字符串[]>;,java,arrays,string,csv,Java,Arrays,String,Csv,我对字符串数组有问题,并试图将其添加到列表中。 下面是用于创建问题的代码 程序在循环的第一次运行中失败,我已经使用OpenCSV验证了来自CSV的输入 List<String[]> output = null; String[] temp; for(int i = 0; i < 13; i++) { temp = reader.readNext(); //read next line i
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);
}
前两行是正确的,按如下方式分开:
温度[0]温度[1]温度[2]
VANC子弹头0.311
VANC BULLET 0.308
问题(如错误读取)出现在:
文件内容如下:
NullPointerException - if the specified element is null and this list does not permit null elements
但正如您从我的输出(第二行)中看到的,数组“temp”不是null,它在每个元素中分别包含“VANC BULLET 0.308”
我被难住了。有没有人有想法或看到我没看到的东西
谢谢据我所知,您从未初始化过
列表输出=null代码>在您的代码中。因此,当它调用List.add时,由于List仍然为空,它抛出NPE
首先初始化它:
List<String[]> output = new ArrayList<String[]>();
List output=new ArrayList();
列表输出
定义为空
List<String[]> output = null;
这不会编译temp=reader.readNext()代码>但我怀疑您没有给出temp
值。
List<String[]> output = new ArrayList<String[]>();
List<String[]> output = null;
List<String[]> output = new ArrayList<String[]>();