1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java NoSuchElementException error

  1. Aug 5, 2011 #1
    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?
     
  2. jcsd
  3. Aug 5, 2011 #2

    Mark44

    Staff: Mentor

    NoSuchElementException is used when an enumeration doesn't have a particular element. I think you want to use NoSuchFieldException.
     
  4. Aug 5, 2011 #3
    i tried to change it to NoSuchFieldException, but I am still getting the same error.
     
  5. Aug 5, 2011 #4

    Mark44

    Staff: Mentor

    What's the error that you're getting?
     
  6. Aug 6, 2011 #5
    Cannot find symbol
     
  7. Aug 6, 2011 #6

    I like Serena

    User Avatar
    Homework Helper

    Hi magnifik! :smile:

    Try adding:
    Code (Text):
    import java.util.NoSuchElementException;
    before using it.
     
  8. Aug 6, 2011 #7
    i got it to work by removing the {} symbols.
     
  9. Aug 7, 2011 #8

    Mark44

    Staff: Mentor

    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.
     
  10. Aug 7, 2011 #9
    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.
     
  11. Aug 7, 2011 #10

    I like Serena

    User Avatar
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java NoSuchElementException error
  1. Java compiler error (Replies: 3)

  2. HeapSort Java (Replies: 3)

Loading...