0

For my class I have to create an iterator that will generate a power set of a LinkedSet. Per my professor, I have to iterate through a bitString of the int current. Where current is the subset to which we are adding elements of the main set. I keep having a NullPointerException. I think I am looping through the bitString correctly but when I want to move to the next node in the set, it is saying it is null. I've stared at this for 12 hours and cannot figure out what to do. My professor provided a framework, so I'll explain what is mine and what is his.

This is the framework:

private class LinkedSetPowerSetIterator implements Iterator<Set<T>> {
    // cardinality of this set
    int N;

    // cardinality of the power set (2^N)
    int M;

    // the integer identifier of the current subset, i.e. the current element
    // of the power set. Legal values range from 0..M-1 inclusive.
    int current;

    public LinkedSetPowerSetIteratorInt32BitString() {
        // initialize N, M, and current here
    }

    public boolean hasNext() {
        return current < M;
    }

    public Set<T> next() {
        LinkedSet<T> s = new LinkedSet<T>();
        char[] bitstring = Integer.toBinaryString(current).toCharArray();

        // iterate from right to left over bitstring and the internal
        // linked list to ensure that the call to add will insert a new
        // first node (constant time)
        
        current = current + 1;
        return s;
    }

    public void remove() {

    }
}

What I have so far:

private class myPowerIterator implements Iterator<Set<T>> {

      // cardinality of set
      int N;

      // cardinality of power set (2^N)
      int M;

      //current subset in power set
      int current;

      // starting node, rear node
      Node set = rear;
      
     
      public myPowerIterator() {
         N = size;
         M = (int)Math.pow(2, N);
         current = 0;
         
      }
   
      public boolean hasNext() {
      
         return (current < M);
      }
      
      public Set<T> next() {
      
         if (!hasNext()) {
            throw new NoSuchElementException();
         }  
         
         LinkedSet<T> result = new LinkedSet<T>();
         
                    
         char[] bitString = Integer.toBinaryString(current).toCharArray();
         
         for (int i = bitString.length - 1; i >= 0; i--) {
            if (bitString[i] == 1) {
               result.add(set.element); // How do I make sure it is adding elements
               set = set.prev;          // from starting set? Keep getting Error:                          
            }                           // NullPointerException: Cannot read field
            else {                      // "prev" because this.set is null.
               set = set.prev;
            }
         }  
      
         
         current = current + 1;           
         return result;
                    
      }
   
      public void remove() {
         throw new UnsupportedOperationException();
      }
   }

1 Answers1

0

You need to implement the Iterable<T> interface in your LinkedSet<T>. If you don't do this then your next() method will not "reset" it's location, thus giving you the NullPointer.