I want to sum up all the values of a linked list recursively but it doesn't work. It says:
Cannot invoke "Element.sum()" because the return value of "Element.getNext()" is null
public class Element{
private int value;
private Element next;
}
public class MyList{
private Element elements;
public int sum(){
if (elements == null) return 0;
return elements.getValue() + elements.getNext().sum();
}
}
}