2

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);
2
  • 3
    Where is rbag declared and initialized? Commented Apr 21, 2015 at 15:28
  • Sorry added in the code for initializing. Commented Apr 21, 2015 at 15:32

3 Answers 3

9

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)

Sign up to request clarification or add additional context in comments.

1 Comment

for throwing an exception rather than returning a null+1
4

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.

Comments

1

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();
}

1 Comment

Yep that worked. Thanks. I thought null didn't seem right and I actually tried returning this, just didn't have the rest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.