0

I have the following code:

public static void printCollection(ArrayList<Data> al, LinkedList<Data> ll, Stack<Data> stack){

    for(Iterator<Data> iter = al.iterator(); iter.hasNext();){
        Data x = (Data)iter.next();
        x.print();
    }

    System.out.println();

}

public static void main(String[] args){

    Data x = new Data("Fred", 41);
    x.print();

    //ArrayList
    ArrayList<Data> arrayList = new ArrayList<Data>();

    //LinkedList
    LinkedList<Data> linkedList = new LinkedList<Data>();

    //Stack
    Stack<Data> stack = new Stack<Data>();

    //'people' variables
    Data person1 = new Data("Fred", 21);
    Data person2 = new Data("Jane", 21);
    Data person3 = new Data("Zoe", 23);
    Data person4 = new Data("Harry", 78);

    //ArrayList
    arrayList.add(person1);
    arrayList.add(person2);
    arrayList.add(person3);
    arrayList.add(2, person4);

    printCollection(arrayList, null, null);

    //LinkedList
    linkedList.add(person1);
    linkedList.add(person2);
    linkedList.add(person3);
    linkedList.add(2, person4);

    printCollection(null, linkedList, null);

    //Stack
    stack.push(person1);
    stack.push(person2);
    stack.push(person3);
    stack.push(person4);

    while(stack.isEmpty() == false)
    {
        stack.pop().print();
    }
    System.out.println(stack.size());

}

...which produces a NullPointerException error. However, if I remove some lines of code to make it look like this:

public static void printCollection(ArrayList<Data> al, LinkedList<Data> ll, Stack<Data> stack){

    for(Iterator<Data> iter = al.iterator(); iter.hasNext();){
        Data x = (Data)iter.next();
        x.print();
    }

    System.out.println();

}

public static void main(String[] args){

    Data x = new Data("Fred", 41);
    x.print();

    //ArrayList
    ArrayList<Data> arrayList = new ArrayList<Data>();

    //LinkedList
    LinkedList<Data> linkedList = new LinkedList<Data>();

    //Stack
    Stack<Data> stack = new Stack<Data>();

    //'people' variables
    Data person1 = new Data("Fred", 21);
    Data person2 = new Data("Jane", 21);
    Data person3 = new Data("Zoe", 23);
    Data person4 = new Data("Harry", 78);

    //ArrayList
    arrayList.add(person1);
    arrayList.add(person2);
    arrayList.add(person3);
    arrayList.add(2, person4);

    printCollection(arrayList, null, null);

}

}

...then it runs just fine. I have checked multiple times and have been unable to detect the error (no pun intended (NullPointerException)).

The error keeps appearing on the following line:

for(Iterator<Data> iter = al.iterator(); iter.hasNext();){
//remaining code omitted for illustration purposes

I have no idea what it could be and need some fresh eyes to help me and take a look.

Thank you for taking the time to read this.

Mick

3
  • What does the stack-trace tell? At which line is the Exception thrown? Commented Mar 2, 2011 at 20:12
  • @Boris Hi Boris, I updated the description as soon as I posted with the info as I forgot to include it - it is now correctly-described. Thank you. Commented Mar 2, 2011 at 20:13
  • Great. Though answers have already been posted now :-) Commented Mar 2, 2011 at 20:17

2 Answers 2

3

In case one, you are passing in null for the only collection that is being used: al, which of course throws an NPE when you ask it for an iterator. That doesn't happen in case two.

Another note: you don't need to cast your iter.next() call.

To fix this, you might want to change the signature of printCollection to:

public static void printCollection(Collection<Data> c){
 for(Iterator<Data> iter = c.iterator(); iter.hasNext();) iter.next().print();
 System.out.println();    
}

since that's (1) what's going on the method and (2) reflects the naming better. The compiler will force you to make some cleanup, and doing that correctly will eliminate the problem or make it painfully more obvious where the problem is.

Sign up to request clarification or add additional context in comments.

2 Comments

I see...was it 'null' which was causing the errors? I did not list the others as I thought that it would not work. So in each case, I need to list 'arrayList', 'linkedList' and 'stack', right? As opposed to 'null', 'linkedList' and 'stack', for example? Does that make sense, and if so, is that right?
yes...sort of. It'll fix the null problem, but I don't think it will fix a structural problem with your code - that it's not doing anything with linkedList or stack.
0

At one point you're calling:

printCollection(null, linkedList, null);

and look at your method's declaration. It tries to obtain an iterator from the first param. This is causing your NPE.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.