开发者

nullPointerException I cannot figure out in Java generic class

开发者 https://www.devze.com 2023-02-15 12:56 出处:网络
I am trying to write program and keep开发者_JAVA百科 getting a nullPointerException when I call a particular method, what does this mean ?I think it should be

I am trying to write program and keep开发者_JAVA百科 getting a nullPointerException when I call a particular method, what does this mean ?


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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消