[英]null pointer exception on adding string object to arraylist
Cant get around this. 不能绕过这个。 Null Pointer exception occurs when trying to add a String object to an ArrayList. 尝试将String对象添加到ArrayList时发生空指针异常。 Trouble is, I believe I have already initialized the 'temp' ArrayList properly. 麻烦的是,我相信我已经正确初始化了'temp'ArrayList。 Please help. 请帮忙。
void initialConbinations(String[] headers) {
//Dataset is SN,FN,RN,PN
//Map the headers to the appropriate portions
String[] newHeaders = {"SN,high", "SN,medium", "SN,low", "FN,high", "FN,medium", "FN,low", "RN,high", "RN,medium", "RN,low"};
List<List> assRuleHeaders = new ArrayList<List>();
ArrayList<String> temp = new ArrayList<String>();
//Use bitwise shifting to do the rest
int k = 0, l = 0;
for (int i = 1; i < 511; i++) {
for (int j = 0; j < 9; j++) {
l = i;
k = (l >> j) & 0x00000001;
if (k == 1) {
System.out.println(k + " , " + i + " , " + j);
System.out.println(newHeaders[j]);
temp.add(newHeaders[j]); //Getting a Null Pointer Exception here
}
}
assRuleHeaders.add(temp);
temp = null;
}
for (int i = 0; i < assRuleHeaders.size(); i++) {
this.printArray(assRuleHeaders.get(i));
}
}
The error is with the line 该错误与该行有关
temp = null;
You are not reinitializing temp
again. 你是不是重新初始化temp
试。
if you move the line 如果你移动线
ArrayList<String> temp = new ArrayList<String>();
between the two for loops all should be fine. 在两个for循环之间都应该没问题。
At the last step of i
loop you are setting temp
to null instead of setting it to a new array using new ArrayList<String>();
在i
循环的最后一步,您将temp
设置为null,而不是使用new ArrayList<String>();
将其设置为新数组new ArrayList<String>();
. 。 This will cause temp.add(newHeaders[j]);
这将导致temp.add(newHeaders[j]);
to through the null pointer exception. 通过空指针异常。
change 更改
temp = null;
to 至
temp = new ArrayList<String>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.