0

I'm having some trouble with a simple NullPointerException when trying to construct my own iterator for a HashTable, and I was wondering if this is the proper way to do it. I seem to get errors at the allEntries.add part of the code. Any help would be really appreciated!!

public class HashIterator implements  Iterator<HashNode<K,V> > {
    HashLinkedList<K,V>  allEntries;

    /**
     * Constructor:   make a linkedlist (HashLinkedList) 'allEntries' of all the entries in the hash table
     */
    public  HashIterator()
    {

        //  ADD YOUR CODE BELOW HERE


        for (int i=0; i<numBuckets; i++) {
            HashLinkedList<K,V> currentBucket = buckets.get(i);

            HashNode<K,V> curNode = currentBucket.getHead();
            while (curNode != null) {

                allEntries.add(curNode.getKey(), curNode.getValue());
                curNode = curNode.getNext();
            }

        }

        //  ADD YOUR CODE ABOVE HERE

    }
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
vickicm
  • 9
  • 1
  • Show the error please... but at first glance it seems that numBuckets is never initialised. – Ivonet Dec 10 '17 at 17:43

1 Answers1

0

OK. You don't initialize allEntries before using it.

You need to do something like

allEntries = new HashLinkedList<>();
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45