2

I have following code snippet:

@Override
public Message next() {
    if (!this.hasNext()) {
        throw new NoSuchElementException();
    }
    // return statement
}

However, findbugs says that there is BAD_PRACTICE:

It: Iterator next() method can't throw NoSuchElementException (IT_NO_SUCH_ELEMENT) This class implements the java.util.Iterator interface. However, its next() method is not capable of throwing java.util.NoSuchElementException. The next() method should be changed so it throws NoSuchElementException if is called when there are no more elements to return.

I do not understand how to fix the code to pass findbugs check. Any ideas?

  • Is it really the `NoSuchElementException` from `java.util`? Or have you maybe imported a different one by accident? – barfuin Jan 15 '16 at 12:10
  • Yes, it's java.util.NoSuchElementException – Andrey Eliseev Jan 15 '16 at 18:20
  • Then we'd need to see your complete class, or better the simplest class with which you can reproduce the behavior. It could be a shortcoming of the detector, or some part of your code. – barfuin Jan 15 '16 at 20:32

1 Answers1

1

Try to make mvn clean on the project. I have the same issue because SpotBugs uses previously compiled class without changes. So mvn clean or manually deleted this class helps me.

Update. Also, you probably don't need to invoke hasNext() from next() method.