Dear Forum,
I have written this neat Java Class as part of my preparation for next week's exams. Somehow, the function IntList get(int i), which is supposed to return a reference to the i-th element of the List throws a null pointer exception and i cant figure out why.
I have gone through the code several times and I am quite certain the get() method is correct. The mistake must be somewhere else. Can you help me?
IntList.txt
IntList.txt
public class IntList { private int info; //the int data of this list element private IntList next; //the rest of the list /** * Sets up a new instance of IntList corresponding to the given info and next. * @param info the int data of this list element * @param next the rest of the list */ public IntList(int info, IntList next) { this.info = info; this.next = next; } /** * A new list where the given info has been prepended. * @param info the int data of the new list element * @return a new instance of IntList */ /* public IntList prepend(int info) { return new IntList(info, this); } */ /** * A new list where the given info has been appended. * @param info the int data of the new list element * @return a new instance of IntList */ public IntList append(int info) { if(next == null) { return new IntList(this.info, new IntList(info, null)); } else { return new IntList(this.info, next.append(info)); } } public IntList get(int i) { int counter=0; IntList current=this; while(counter < i) { current=current.next; } counter++; return current; } /** * Commputes the sum of all elements of this list. * @return the sum of all elements */ public int sum() { if(next == null) { return info; } else { return info + next.sum(); } } /** * Auxiliary function for the reversal of this list. * @param acc the list elements accumulated so far * @return a new instance of IntList */ private IntList reverseAux(IntList acc) { if(next == null) { return new IntList(info, acc); } else { return next.reverseAux(new IntList(info, acc)); } } /** * A new list with the elements of this list in reverse order. * @return a new instance of the IntList */ public IntList reverse() { return reverseAux(null); } /** * String representation of this list. */ @Override public String toString() { if(next == null) { return "" + info; } else { return info + " , " + next; } } /** * An integer array is converted to a list * @param values is an array containing integer elements * @return a new instance of IntList */ public static IntList fromArray(int[] values) { int n = values.length; IntList res = new IntList(values[0] , null); for(int i = 1; i < n ; i ++) { res = res.append(values[i]); } return res; } /** * The length of a given IntList object is determined * @return the length of the list */ public int length() { int counter = 1; while(next != null) { counter = counter + 1; next = next.next; } return counter; } /** * A reference to the i-th element of a list is returned * @param integer that tells the function which element shall be returned * @return A reference to the i-th element */ /*public IntList map(IntList list) { int shorter_list = 0; IntList ret = new IntList(0 , null); if(list.length <= this.length) { shorter_list = list.length; } else { shorter_list = this.length; } for(int i = 0 ; i < shorter_list ; i ++) { ret.append() } } */ public static void main(String[] args) { IntList lst = new IntList(1, null); for(int i = 2 ; i < 10 ; i ++) { lst = lst.append(i); } System.out.println(lst); System.out.println(lst.reverse()); System.out.println(lst.sum()); int[] values = new int[4]; values[0] = 3; values[1] = 4; values[2] = 5; values[3] = 8; System.out.println(fromArray(values)); System.out.println(fromArray(values).length()); System.out.println(lst.length()); System.out.println(lst.get(3)); System.out.println(lst.get(5)); } }