Frank Burns
Frank Burns

Reputation: 35

nullPointerException I cannot figure out in Java generic class

I am trying to write program and keep getting a nullPointerException when I call a particular method, what does this mean ?

Upvotes: 1

Views: 363

Answers (1)

Stan Kurilin
Stan Kurilin

Reputation: 15792

I think it should be

private int size; //non static

private static <S extends Comparable<S>> MyList<S> leftHalf(MyList<S> list) {
    MyList<S> leftSide = new MyList<S>();
    int middle = list.size() /2;
    for (int countToMiddle = 0; countToMiddle < middle; countToMiddle++) {
        leftSide.addEnd(list.head());
    }

    return leftSide;
}

if no, please provide more information about what this method should do.

upd: construction issue

public MyList() {   //takes no arguments
    nodes = null;
}
public MyList(T... args) {  //takes any number of arguments
    this();
    for(T t : args){
        add(t);
    }
}

upd: addEnd issue

public void addEnd(T item) {
    if (nodes == null) {
        nodes = new NodesList<T>(item, null);
        return;
    }
    if (nodes.tail == null) {
        nodes.tail = new NodesList<T>(item, null);
    } else {
        nodes.tail == new NodesList<T>(nodes.tail, item);
    }
}

Upvotes: 1

Related Questions