-3

I'm a beginner in programming and I need to write some kind of own LinkedList, but only with add(E element) method and with Iterator's hasNext() and next(). Here is my code:

  public class LinkedArray<E> implements Iterator<E> {

        private int size = 0;

        private int current = 0;

        private Node<E> first;

        private Node<E> last;

        private Objects[] objects = new Objects[10];

        public void add(E value) {
            Node<E> element = new Node<E>(last, value, null);
            if (last != null) {
                element.next = element;
            } else {
                first = element;
            }
            last = element;
            size++;
        }

        @Override
        public boolean hasNext() {
            boolean result = false;
            try {
                if (objects[current + 1] != null) {
                    result = true;
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                result = false;
            }
            return result;
        }

        @Override
        public E next() {
            E result;
            try {
                current++;
                result = (get(current - 1));
            } catch (ArrayIndexOutOfBoundsException a) {
                throw new NoSuchElementException("No more elements in list.");
            }
            return result;
        }

   public E get(int position) throws NullPointerException {
        Object result;
    if (this.objects[position] != null) {
        result = this.objects[position];
    } else {
        throw new NullPointerException("Position is empty.");
    }
    return (E) result;
}


        private class Node<E> {

            private E element;

            private Node<E> next;

            private Node<E> prev;

            Node(Node<E> prev, E element, Node<E> next) {
                this.element = element;
                this.next = next;
                this.prev = prev;
            }
        }
    }

But when I started to test add(E value)...

@Test
    public void test() {
        LinkedArray<String> arr = new LinkedArray<>();
        String string = "Test";

        arr.add(string);
        String result = arr.next();

        assertThat(result, is("Test"));
    }

...I only get an error. What the problem is, why am I getting it wrong?

blackHorsie
  • 127
  • 10

2 Answers2

2

You've explicitly thrown your own NPE.

public E get(int position) throws NullPointerException {
    Object result;
    if (this.objects[position] != null) {
        result = this.objects[position];
    } else {
        throw new NullPointerException("Position is empty.");
    }
    return (E) result;
}

If you want to follow the get() method contract of a List, the Javadoc says this

Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

Therefore since you are not "referencing" anything that is null, and rather just going to return null in the case that your array is empty, then throw the other exception.

public E get(int position) throws IndexOutOfBoundsException {
    if (position < 0 || position >= this.objects.length) {
        throw new IndexOutOfBoundsException();
    }
    return (E) this.objects[position];
}

Note: Iterator classes don't usually have a E get() method. Just hasNext() and next()

Therefore, you should not implement your class in a way that next() requires a call to get().

You also need no try-catch there. You already know when position is out of bounds using if-statements.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Your get(...) function reads the objects from the objects array. You never set the contents of this array so if position is less then 10 it will always be null and cause the NPE you encountered.

It looks like you've adapted an array-based list but only changed the add method. All the other methods interact with the empty objects array.

Kiskae
  • 24,655
  • 2
  • 77
  • 74