Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difficulty throwing NoSuchElementException in Java

Tags:

java

exception

this is my first attempt to a code doubly linked program in java:

This is my implementation of getting an iterator to getall items in the doubly linkedlist

public Object next()  {
    if(list.getSize()==0){
     throw new NoSuchElementException();

    }else{ 
        current=current.getNext();
        return current.getItem();
    } 
}

Please dont laugh at me but whatever I try I am getting

Cannot find symbol : symbol class: NoSuchElementException

I tried creating a class NoSuchElementException.java which extends Exception

Then I get

unreported exception NoSuchElementException; must be caught or declared to be thrown throw new NoSuchElementException();

I tried changing code to :

public Object next() throws NoSuchElementException {

Then I get

next() in ElementsIterator cannot implement next() in java.util.Iterator; overridden method does not throw NoSuchElementException

Can anybody point to where I got wrong. If this is not sufficient info to resolve this issue, please do tell me.

like image 977
sreeprasad Avatar asked Oct 02 '11 23:10

sreeprasad


People also ask

How do I fix Java Util NoSuchElementException?

To fix the NoSuchElementException , it should be ensured that the underlying object contains more elements before using accessor methods that can throw the exception.

What causes NoSuchElementException in Java?

The NoSuchElementException in Java is thrown when one tries to access an iterable beyond its maximum limit. The exception indicates that there are no more elements remaining to iterate over ​in an enumeration.

How do I get NoSuchElementException?

Cause for NosuchElementException If you call the nextElement() method of the Enumeration class on an empty enumeration object or, if the current position is at the end of the Enumeration, a NosuchElementException is generated at run time.


2 Answers

You need to import java.util.NoSuchElementException instead of creating your own.

like image 160
SLaks Avatar answered Oct 22 '22 13:10

SLaks


The compiler is giving you a pretty good explanation. You are providing an implementation of the next() method declared in Iterator, which has a signature of:

public Object next()

However, you have introduced your own checked exception called NoSuchElementException, which required that you change the method signature to:

public Object next() throws NoSuchElementException

Now you are no longer implementing the method declared in the interface. You've changed the signature.

Anyways, there are a couple ways you can fix this:

  1. Change your NoSuchElementException class so that it inherits from RuntimeException instead of just Exception. Then you don't need to add a throws declaration to the method signature.

  2. Get rid of your custom exception type and instead use Java's built-in NoSuchElementException, which already inherits from RuntimeException. This is almost certainly the better option.

like image 33
aroth Avatar answered Oct 22 '22 14:10

aroth