john
john

Reputation: 17

Getting a NullPointerException when trying to create ArrayList

I'm working on an arraylist that will add random tiles to a bag for a scrabble game. My teacher has done a horrible job of explaining things so I'm having to work through this all. I've created constructors for the bag and am attempting to create the bag but am getting a null pointer exception when calling.

Exception in thread "main" java.lang.NullPointerException at h2.RandBag.add(RandBag.java:11)

Heres the code in main returning the error:

    for (Integer ival : rbag)
      System.out.println(ival);

And heres the methods I'm using for constructing.

private ArrayList<E> bag; // container
private Random rand;      // random generator

//constructors
public RandomBag(){
  this.bag = new ArrayList<E>();
  this.rand = new Random();

public RandomBag(int seed) {
  this.bag = new ArrayList<E>();
  this.rand = new Random(seed);

And the iterator since it returns null and that could be the problem.

public Iterator<E> iterator() {

    return null;
}

I've tried returning different things but get an error each time. Iterators haven't been explained all that much so I might be missing something.

RandomBag<Integer> rbag = new RandomBag<Integer>(17);
for (int i = 0; i < 8; ++i)
rbag.add(10+i);

Upvotes: 2

Views: 327

Answers (3)

Claudio
Claudio

Reputation: 1858

Writing this:

 for (Integer ival : rbag)
      System.out.println(ival);

Is the same as writing this:

for (Iterator<Integer> iterator = rbag.iterator(); iterator.hasNext(); ) {
   Integer ival = iterator.next();
   System.out.println(ival);
}

It's just a syntax sugar. So if you return a null iterator from your collection (rbag) it will allways end in a NullPointerException (java will try to ask that iterator for .hasNext() immediatly after that)

I suggest you do something like

public Iterator<E> iterator() {
    return this.bag.iterator();
}

Upvotes: 1

yshavit
yshavit

Reputation: 43391

Yup, the iterator() returning null is the problem.

That for loop is basically sugar for:

Iterator<Integer> iter = rbag.iterator();
while (iter.hasNext()) {
    Integer ival = iter.next();
    System.out.println(ival);
}

If iterator() returns null, then the iter.hasNext() invocation will result in the NullPointerException.

Upvotes: 4

Lukas Eder
Lukas Eder

Reputation: 220807

A better iterator() implementation

Although, I don't know all of your requirements and your current implementation, I'm pretty sure that this here's a better implementation:

public Iterator<E> iterator() {
    return bag.iterator();
}

Explanation of the foreach loop

Note that this:

for (Integer ival : rbag)
    System.out.println(ival);

... is just syntax sugar for this:

Iterator<Integer> it = rbag.iterator();
while (it.hasNext())
    System.out.println(it.next());

This will then explain the NullPointerException

Best Practice

Note that if you don't want to completely implement an interface like List, instead of simply returning null, you should better throw an exception:

public Iterator<E> iterator() {
    throw new UnsupportedOperationException();
}

It will then be much clearer why things don't work (yet)

Upvotes: 9

Related Questions