简体   繁体   中英

iterating through list gives a NullPointerException

I defined a nested Node class inside a class, and

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.

    _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

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.

    _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.)

Then I wanted to define a getPosition method inside 'Structure' class. When I tested this method, it throws a 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.

I guess the for loops of pos.getNext(); 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? 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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM