[英]iterating through list gives a NullPointerException
I defined a nested Node class inside a class, and 我在一个类中定义了一个嵌套的Node类,并且
class HNode<T extends ArrayList> {
private T _datum;
private HNode<T> _prev;
private HNode<T> _next;
public HNode(T datum, HNode<T> prev, HNode<T> next) {
_datum = datum;
_prev = prev;
_next = next;
}
public HNode<T> getNext() {
return _next;
}
and I designated _datum, _prev, and _next fields manually when I created this Node class. 并且在创建此Node类时,我手动指定_datum,_prev和_next字段。
_r0 = new HNode<ArrayList<String>>(_al.get(3), _r0, _r1);
_r1 = new HNode<ArrayList<String>>(_al.get(4), _r0, _r2);
_r2 = new HNode<ArrayList<String>>(_al.get(5), _r1, _pos3);
_pos3 = new HNode<ArrayList<String>>(_al.get(6), _r2, _pos4);
_pos4 = new HNode<ArrayList<String>>(_al.get(7), _pos3, _pos5);
(this is a part of the initialization.) (这是初始化的一部分。)
Hi. 你好 I defined a nested Node class inside a class named 'Structure', and 我在名为“ Structure”的类中定义了一个嵌套的Node类,
class HNode<T extends ArrayList> {
private T _datum;
private HNode<T> _prev;
private HNode<T> _next;
public HNode(T datum, HNode<T> prev, HNode<T> next) {
_datum = datum;
_prev = prev;
_next = next;
}
public HNode<T> getNext() {
return _next;
}
and I designated _datum, _prev, and _next fields manually when I created this Node class. 并且在创建此Node类时,我手动指定_datum,_prev和_next字段。
_r0 = new HNode<ArrayList<String>>(_al.get(3), _r0, _r1);
_r1 = new HNode<ArrayList<String>>(_al.get(4), _r0, _r2);
_r2 = new HNode<ArrayList<String>>(_al.get(5), _r1, _pos3);
_pos3 = new HNode<ArrayList<String>>(_al.get(6), _r2, _pos4);
_pos4 = new HNode<ArrayList<String>>(_al.get(7), _pos3, _pos5);
(this is a part of the initialization. _al is a ArrayList of ArrayList, for which I used to call Collections.shuffle(_al) method.) (这是初始化的一部分。_al是ArrayList的ArrayList,我曾经为此调用过Collections.shuffle(_al)方法。)
Then I wanted to define a getPosition method inside 'Structure' class. 然后,我想在“ Structure”类中定义一个getPosition方法。 When I tested this method, it throws a NullPointerException. 当我测试此方法时,它将引发NullPointerException。
The reason I partitioned this method with if statements is that the 2th and 3rd, 16th and 17th Nodes are not connected to each other. 我用if语句划分此方法的原因是第2个和第3个,第16个和第17个节点未相互连接。
I guess the for loops of pos.getNext(); 我猜pos.getNext()的for循环; might be the cause of the problem, not the conditional statements, though. 可能是问题的原因,而不是条件语句。
public HNode<ArrayList<String>> getPosition(int index) {
HNode<ArrayList<String>> pos = null;
if(index<3) {
pos = _l0;
for(int i=0; i<index; i++) {
pos = pos.getNext();
}
}
else if(3<=index && index <= 16) {
pos = _r0;
for(int i=0; i<(index-3); i++) {
pos = pos.getNext();
}
}
else if(index <= 17 && index <= 24) {
pos = _w1;
for(int i=0; i<(index-17); i++) {
pos = pos.getNext();
}
}
return pos;
}
Do you know why this gives a NullPointerException? 您知道为什么会给出NullPointerException吗? I am trying to figure this out for a while but could not. 我试图解决这一问题,但不能。 Please give me an advice. 请给我一个建议。 Thank you! 谢谢!
The error occurs when I call getNext() method. 当我调用getNext()方法时发生错误。 Is there any problem? 有什么问题吗?
From personal experiance of null references, it seems likely that 从空引用的个人经验来看,
_r0 = new HNode
_pos3 = new HNode etc
Is null. 一片空白。 Have you attempted using the debugger? 您是否尝试过使用调试器?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.