1

I do not understand why a java.lang.NullPointerException is thrown with the following code (at this line: iterator = chapterKeywords[z].iterator();):

Iterator<String> iterator;

        for (int z = 0; z < chapterKeywords.length; z++) {

            try {

                iterator = chapterKeywords[z].iterator();
                //exception thrown here after first iteration (i.e., when z = 1) and subsequent ones

                while (iterator.hasNext()) {

                    System.out.println(iterator.next());

                }

            } catch (Exception e) {
               ;
            }

        }

Although I get the NullPointerException, the code fully executes. FYI, chapterKeywords is a LinkedList Array where each array element is a LinkedList (e.g., singleChapterKeywords).

LinkedList<String>[] chapterKeywords = new LinkedList[numFiles];
LinkedList<String> singleChapterKeywords = new LinkedList<String>();

Although the program seems to run fine, I would like to understand why the NullPointerException is thrown as I would like to completely eliminate it. Basically, the exception is thrown at the start of each iteration in the for-loop. I've modified the code to create a new iterator inside the loop, create an array of iterators, etc but they all yield the same result: NullPointerException at iterator = chapterKeywords[z].iterator();.

Any help or insight into eliminating this exception is greatly appreciated.

So, here are my questions:

Why is there a NullPointerException thrown at each subsequent iteration when the iterator variable is being assigned to a new iterator?

How can I eliminate this exception?

Once again, TIA!

Update: I thought that the LinkedList's were properly populated and worked fine, but, alas, this was not the case! Thank you all for your immeasurable help!

3
  • 5
    chapterKeywords[z] is null for one or more values of z Commented Aug 8, 2012 at 20:18
  • Thanks. Most certainly, chapterKeywords[z] was all hosed up! Commented Aug 8, 2012 at 21:55
  • To make matters worse I had accidentaly included the above loop into another loop (missed it by a bracket!), resulting in the squaring of the number of iterations (numFiles^2 instead of numFiles!)... Commented Aug 9, 2012 at 0:15

2 Answers 2

5

Looks like you've populated the lists but missed to add (some of) them to the array.

This will reveal your problem:

for (int z = 0; z < chapterKeywords.length; z++) {
   if (chapterKeywords[z] == null) {
      System.out.println("No list found at array index " + z);
      continue;
   }
   iterator = chapterKeywords[z].iterator();
   while (iterator.hasNext()) {
      System.out.println(iterator.next());
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks! Your code helped me understand that the problem layed with chapterKeywords[z] and not the iterator. Once again, many thanks for your help!
1

chapterKeywords[z] is null. It definitely exists or you would get an out of bounds error, but you didn't actually ever call something like chapterKeywords[z] = new String()..

1 Comment

Thanks. Indeed, chapterKeywords[z] was all hosed up!

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.