Results 1 to 6 of 6
  1. #1
    anders10 is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default iterator inner class in minheap

    Hello!

    I have a minheap class that is implemented as a priorityqueue and it works fine as far as to the iterator that I have to implement as a innerclass. But I can't get it to work properly.
    Here is part of the code:

    public class MinHeap<E> extends AbstractQueue<E> implements Queue<E> {

    public HeapEntry<E>[] table;
    private static final int CAPACITY = 10;
    private int currentSize = 0;
    Comparator<E> comparator = null;

    @SuppressWarnings("unchecked")
    public MinHeap() {

    table = (HeapEntry<E>[]) new HeapEntry[CAPACITY];
    }

    @SuppressWarnings("unchecked")
    public MinHeap(Comparator<E> comp) {
    comparator = comp;
    table = (HeapEntry<E>[]) new HeapEntry[CAPACITY];
    }


    //In order to make the priority queue to work properly we implement this inner class HeapEntry
    public static class HeapEntry<E> {
    private E element;
    public int index;

    public HeapEntry(E element) {
    this.element = element;
    }

    public int getIndex(){
    return index;
    }
    }
    //Here is my iterator class that I don't know if it's correct

    private class MyListIterator implements Iterator<E>{
    private HeapEntry<E>[]a;
    private int pos;
    private MyListIterator(){
    pos = 0;
    }
    public boolean hasNext(){
    if (pos < a.length)
    return true;
    else
    return false;
    }
    public E next(){
    if (this.hasNext())
    return a[pos++].element;
    else throw new NoSuchElementException();
    }

    public void remove(){
    throw new UnsupportedOperationException();
    }
    }
    //Here is the iterator method in the outer class. There is of course many more methods but I don't think that I have to show them here. They work properly.

    @Override
    public Iterator<E> iterator() {
    return new MyListIterator();
    }

    //I do my testing in JUnit

    @Test
    public final void testIterator() {
    mh.offer(4);
    mh.offer(1);
    mh.offer(25);
    mh.offer(14);
    mh.offer(11);
    Iterator<Integer> myItr = mh.iterator();
    while (myItr.hasNext()){
    Integer i = new Integer(1);
    //HeapEntry<Integer>table[];
    i = myItr.next();
    mh.poll();
    }
    assertEquals("key not found in map: ", new Integer(1), mh.peek());
    assertFalse("isEmpty true for non empty set", mh.isEmpty());
    assertEquals("wrong size():", 4, mh.size());
    }

    // But here I get a NullPointerException at while(myItr.hasNext()) Can anyone tell me what is wrong here?
    Thanks a lot, Anders

  2. #2
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    This is because "myItr" is null. This can be caused due to "mh".
    What is "mh"? Where is it declared?

    Maybe you didn't post this. If so, check what [return new MyListIterator();] is actually returning.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Looks like the NPE is actually in the hasNext method since the array a is never created.

  4. #4
    anders10 is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    Hello, thanks for answering.

    mh is declared in the JUnit class:
    public class testMinHeap extends TestCase {

    MinHeap<Integer>mh;

    protected void setUp() throws Exception {
    super.setUp();
    mh = new MinHeap<Integer>();
    }

    And it is as you say that the NPE is in the hasNext method and myListIterator is returning an Exception. I am running the debugger and when I'm coming down to the the iterator method which is supposed to return the myListIterator object it becomes an exception.

    There is propably something wrong with my declarations. MyListIterator is An inner class. Is it something here?

    Junky says that array a is never created but should I create a new array?
    Eclipse doesn't take this:

    private HeapEntry<E>[]a;
    private int pos;
    public MyListIterator(){
    pos = 0;
    a = new HeapEntry<E>[];
    }
    Eclipse says: cannot create a generic array out of MinHeap.HeapEntry<E>.

    Do you have any clue ?

    Thanks for taking you time!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

  6. #6
    anders10 is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    Hello!

    Sorry about that. I didn't know about the rules. Next time I will tell you if I've posted my problem at another site. B ut anyway. Thank you for the help. I managed to solve the problem.

    I had an array []a in my MyListIterarator class. It shouldn't be there at all.

    Thank you again.

Similar Threads

  1. Iterator
    By Dayanand in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 01:17 PM
  2. for..iterator
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 03:12 PM
  3. Iterator next import class?
    By daydreamer in forum New To Java
    Replies: 7
    Last Post: 10-05-2010, 12:32 AM
  4. Replies: 0
    Last Post: 05-18-2010, 01:53 AM
  5. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 03:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •