1

I am trying to simply get input from the user, in a method that returns Object. For some reason, this error is thrown:

Exception in thread "main" java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61) Caused by: java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at candle.Main.getValue(Main.java:381) at candle.Main.runFile(Main.java:979) at candle.Main.main(Main.java:1013) ... 5 more

Here is a code snippet:

System.out.print("> ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
scanner.close();

Can anyone explain how to get this work, or point out something I have done wrong? Thanks.

MisterHazza
  • 188
  • 1
  • 10
  • 1
    Does this answer your question? [java.util.NoSuchElementException: No line found](https://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found): with Scanner you need to check if there is a next line with `hasNextLine()`. Here is the Javadoc: https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html – FoggyDay Jan 07 '20 at 19:31
  • 1
    Nothing in that code would explain that exception. Are you perhaps creating another scanner around system.in and closing it prior to this code? – azurefrog Jan 07 '20 at 19:31
  • I am creating another Scanner object in the main() method, and it is closed using scanner.close(), and it is working fine. Is that why it is calling an error? – MisterHazza Jan 07 '20 at 19:33
  • Calling `close()` on a scanner also closes the underlying stream. If you're closing that other scanner, it will cause the error you're seeing. – azurefrog Jan 07 '20 at 19:35
  • Ah ok I see, how would I get multiple user inputs at different times then? – MisterHazza Jan 07 '20 at 19:36
  • You can just reuse the same scanner, or just don't close them when you're done (this will cause a warning, though). – azurefrog Jan 07 '20 at 19:37
  • Ok thanks very much! you have helped a lot! – MisterHazza Jan 07 '20 at 19:39

1 Answers1

2

This question has been asked before.

  1. The basic answer is:

    • Scanner will throw java.util.NoSuchElementException: No line found if there's no line available to read.

    • The solution is:

      Scanner scanner = new Scanner(System.in);
      String input = null;
      if (scanner.hasNextLine()
          scanner.nextLine();
      
  2. The problem might also occur:

    • If you close the scanner prematurely (for example, input.close() in some other method)

    • If your input contains special characters, then you should explicitly specify the desired encoding. EXAMPLE: Scanner scanner = new Scanner(new FileInputStream(filePath), "UTF-8");

All of these issues are discussed here:

FoggyDay
  • 11,962
  • 4
  • 34
  • 48