Questions tagged [nosuchelementexception]

Use this tag if your problem is caused by or primarily involves a NoSuchElementException in Java.

The NoSuchElementException is a RuntimeException thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

It is also thrown by:

  • the various next* methods (nextInt, nextFloat, nextLine,...) of the Scanner class when input is exhausted;
  • the get method of the Java 8 Optional class when a value is not present;
  • the methods firstElement and lastElement in the Vector class when there are no elements;
  • the methods firstKey and lastKey in the TreeMap class when there are no keys;
  • the methods getFirst, getLast, removeFirst and removeLast in LinkedList when there are no elements;

and other collections.

573 questions
2
votes
2 answers

Exception in thread "main" java.lang.NoSuchMethodException: after I add ArrayList

I am writing program that moves digits from TextField to TextArea for sorting or shuffling or reversing them. All entered digits have to be save in ArrayList. At first I create javafx stage with buttons labels and etc. Then I added listeners for…
rob111
  • 1,249
  • 1
  • 13
  • 18
2
votes
2 answers

NoSuchElement using scanner twice

I'm working on a code that reads user input, so I've made a procedure that does just that, reads and return the string. when calling it twice from another procedure, returns a NoSuchElementException error which I can't figure out why. input reader…
user3583107
  • 27
  • 1
  • 2
  • 9
2
votes
1 answer

exception in thread main java.util.nosuchelementexception with new Scanner(file)

I have a problem with my code,I get this error all the time: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1416) at…
Lina
  • 451
  • 1
  • 8
  • 23
2
votes
3 answers

How do i prevent my consumer-threads from removing the last element twice?

Questions: Why do I get a NoSuchElementException when trying to remove the last element? How can I fix that? I have 3 classes (see below) that add/remove Integers to a LinkedList. Everything works fine until the removing Threads get to the…
whateverrest
  • 178
  • 3
  • 11
2
votes
3 answers

Issue with java Scanner not taking nextLine on new instance

package sandbox2; import java.util.Scanner; public class Sandbox2 { public static void main(String[] args) { for (int i = 0; i < 5; i++) { String s = askForProperty("Enter value for " + i + ": "); …
Drifter64
  • 1,103
  • 3
  • 11
  • 30
2
votes
2 answers

Infinite loop on one hand, NoSuchElementException on the other

I recently asked if there is any possible way whatsoever to get an exception from assigning a value to a String variable with the Scanner (The thread is here:) And one of the guys told me that CTRL+D would be a case where a NoSuchElementException…
2
votes
1 answer

Using Scanner to read all files in folder one at a time gives NoSuchElementException

Following Java code: File folder = new File("/home"); File[] listofFiles = folder.listFiles(); for (j = 0; j < listofFiles.length; j++) { File file = listofFiles[j]; if (file.isFile() && file.getName().endsWith(".ucf")) { String content =…
kamalbanga
  • 1,881
  • 5
  • 27
  • 46
2
votes
3 answers

Java NoSuchElementException

So I have a pretty big java application that I wrote a year ago and I'm trying to understand it again. I'm looking at a method in the code where there is an obvious risk of getting NoSuchElementException: I'm calling .next() on a scanner variable…
user2651804
  • 1,464
  • 4
  • 22
  • 45
2
votes
4 answers

Java: NoSuchElementException when iterating through ArrayList

I want to delete duplicate elements and therefore iterate through a ArrayList and compare two consecutive elements. (Persons are comparable) ArrayList persons = getHelper().findAllPersons(); Collections.sort(persons); ListIterator it…
ArtVandelay
  • 97
  • 3
  • 11
2
votes
4 answers

How to locate a list element (Selenium)?

I have the following list:
  • item1 is red
  • item1 is blue
  • item1 is white
I tried the following to print the first item: String item = driver.findElement(By.xpath("//ul//li[0]")).getText(); …
Buras
  • 3,069
  • 28
  • 79
  • 126
1
vote
0 answers

NoSuchElementException on .nextInt();

I am trying to write a program that reads the information of several user bank accounts and performs certain operations on them (withdraw, deposit, show balance) etc. The code seems to be correct, but I am having an issue with the .nextInt();…
catdu
  • 11
  • 3
1
vote
0 answers

How do WebDriverWait throw a NoSuchElementException if there is no element found?

My code does not throw NoSuchElementException even if no element found using WebDriverWait .. import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as…
1
vote
2 answers

How to find and click the "Like" button on Facebook page using Selenium

I am trying to automate the process of liking pages on Facebook. I've got a list of each page's link and I want to open and like them one by one. I think the Like button doesn't have any id or name, but it is in a span class.
Safi K
  • 13
  • 3
1
vote
1 answer

Spring batch multithreading problem with Iterator Item Reader

I'm new to spring batch and still learning, I have batch configuration with IteratorItemReader , Custom Processor and Custom Writer as below, @Autowired JobBuilderFactory jobBuilderFactory; @Autowired StepBuilderFactory…
1
vote
2 answers

How to iterate over xpath and check if image exists and perform operation based on that

Image As you can see in the image, I have 2 solutions (solution 1 is for a flight and solution 2 is for a hotel) I want to iterate over each solution using XPATH. I am successful in that. I want to check the image returned on iteration. My code is…