Java NoSuchElementException error

  • Context: Comp Sci 
  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Error Java
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to the implementation of a doubly linked list in Java, specifically focusing on the use of the NoSuchElementException and the compilation errors encountered. Participants explore potential causes and solutions for the error during the execution of the removeBack method.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an issue with throwing NoSuchElementException when the list is empty and questions if it is being called incorrectly.
  • Another participant suggests that NoSuchElementException is not appropriate and proposes using NoSuchFieldException instead.
  • A participant reports that changing to NoSuchFieldException did not resolve the issue.
  • One participant asks for clarification on the specific error message being received.
  • A participant identifies the error as "Cannot find symbol."
  • Another participant recommends adding an import statement for NoSuchElementException to resolve the issue.
  • One participant claims to have resolved the issue by removing the braces from the if statement.
  • Another participant expresses confusion about the effectiveness of removing braces, stating that the original method appeared correct.
  • A participant suggests adding the braces back to see if the error reoccurs, expressing an expectation that it will not.

Areas of Agreement / Disagreement

Participants express differing opinions on the appropriate exception to use and the impact of braces on the code's functionality. There is no consensus on the underlying cause of the error or the best solution.

Contextual Notes

Participants discuss the implications of code structure and exception handling without resolving the specific reasons for the compilation error. The discussion reflects uncertainty regarding the relationship between the code changes and the error resolution.

magnifik
Messages
350
Reaction score
0
I am writing code for a doubly linked list, but when I try to compile it, I am getting an error when trying to throw the NoSuchElementException() function. Here is the part of the code concerning the issue:

// _size is an integer that keeps track of the number of elements. in this case, i am checking if the list is empty
public Object removeBack() {
if (_size == 0) {
throw new NoSuchElementException();
}
Node node = new Node();
Node cursor = _tail._prev;
cursor._next._prev = cursor._prev;
cursor._prev._next = cursor._next;
_size--;
}

i am wondering.. what's wrong with it? am i calling it incorrectly?
 
Physics news on Phys.org
magnifik said:
I am writing code for a doubly linked list, but when I try to compile it, I am getting an error when trying to throw the NoSuchElementException() function. Here is the part of the code concerning the issue:

// _size is an integer that keeps track of the number of elements. in this case, i am checking if the list is empty
public Object removeBack() {
if (_size == 0) {
throw new NoSuchElementException();
}
Node node = new Node();
Node cursor = _tail._prev;
cursor._next._prev = cursor._prev;
cursor._prev._next = cursor._next;
_size--;
}

i am wondering.. what's wrong with it? am i calling it incorrectly?

NoSuchElementException is used when an enumeration doesn't have a particular element. I think you want to use NoSuchFieldException.
 
i tried to change it to NoSuchFieldException, but I am still getting the same error.
 
What's the error that you're getting?
 
Mark44 said:
What's the error that you're getting?

Cannot find symbol
 
Hi magnifik! :smile:

Try adding:
Code:
import java.util.NoSuchElementException;
before using it.
 
i got it to work by removing the {} symbols.
 
magnifik said:
i got it to work by removing the {} symbols.
They're called braces. In any case, that doesn't make sense to me. Your removeBack method looked fine to me with them, so removing the braces on your if statement shouldn't have made any difference.
 
Mark44 said:
They're called braces. In any case, that doesn't make sense to me. Your removeBack method looked fine to me with them, so removing the braces on your if statement shouldn't have made any difference.

It doesn't make sense to me either, but I didn't change any of my code except for the removal of the brackets and it wasn't throwing an error.
 
  • #10
magnifik said:
It doesn't make sense to me either, but I didn't change any of my code except for the removal of the brackets and it wasn't throwing an error.

So add the curly braces again and check if it throws an error again.
To be honest, I expect that it won't throw an error.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K