1
class Node<T>{

private T myValue;
private ArrayList<Object> next;



public <U> void addLink(U n){
        this.next.add(n);
}


}

in main i have:

    Node<String> myNode1 = new Node<String>("Ciao");
    Node<Integer> myNode2 = new Node<Integer>(12);

    myNode1.addLink(myNode2, true);

I need next in class Node to cointain pointers to the adiacent node

but java throws NullPointerException

Please help me.

2
  • Is myNode1.next actually set to a reference, or is it still null? Commented Nov 17, 2010 at 23:07
  • 1
    Also you can look at the stacktrace that java outputs with this error. It'll tell you exactly where the null pointer occured. Commented Nov 17, 2010 at 23:10

1 Answer 1

4

this.next never got initialized. Try declaring it:

private ArrayList<Object> next = new ArrayList<Object>();
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, although I think ArrayList<Object> is bad form in most circumstances. I suspect user496223 would be better off with an ArrayList<Node<?>>
Or maybe even Node<T> given the name of the next field.
Yeah, I'm not exactly clear on what he had in mind with this. It almost looks like each node could potentially have a different type of object in it, which seems odd to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.