0

I am having a sort of odd problem. So I use BlueJ as my code editor, and it runs my code just fine. When I create a jar file and run it, the program throws the "Exception in "main" java.util.NoSuchElementException: No Line found".

I sort of have 2 questions here:

  1. Why does my code run just fine in BlueJ but not other runtime environments or editors such as Visual Studio Code?

  2. Also why is it failing at all?

Just so everyone is aware, I condensed the code and cut a few parts out. The issue still stands.

import java.util.*;
import java.util.Scanner;
import java.io.*;
/**
* Write a description of class mainBody here.
*
* @author (Brayden Anderson)
* @version (a version number or a date) 
*/
public class mainBody{
    public static ArrayList<String> Messages = new ArrayList<String>();
    public static ArrayList<String> changeLog = new ArrayList<String>();
    /**
     * mainBody Constructor
     * Setup Menu
     */
    public mainBody(){
        Scanner scan = new Scanner(System.in); 
        System.out.println("Welcome to Solar!");
        System.out.println("========================================");
        System.out.println("1. Start Setup");
        System.out.println("2. Quit Program");
        System.out.println("Console: ");
        System.out.println("[System]: Please Start Setup to Proceed");
        String selection = scan.nextLine();
        scan.close();
        if(selection.equals("1")){
            System.out.println("Starting Setup");
        }else if(selection.equals("2")){
            System.exit(1);
        }else{
            new mainBody();
        }
    }
    /**
     * Method main
     *
     * @param args A parameter
     */
    public static void main(String[] args) {
        new mainBody();
    }
} 

output in powershell/CMD:

Welcome to Solar!

  1. Start Setup
  2. Quit Program

Console: [System]: Please Start Setup to Proceed 1 <--User inputed [System]: Starting Setup!... [System]: Would you like to use drive " C " for Installation [System]: Y/N? [Warning]: Setup Halted, Awaiting User Response Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at Setup.autoSearchForDir(Setup.java:309) at Setup.startSetup(Setup.java:24) at mainBody.(mainBody.java:32) at mainBody.main(mainBody.java:489) PS C:\Users\brayd\desktop>

output in Bluej:

Welcome to Solar!

  1. Start Setup
  2. Quit Program Console: [System]: Please Start Setup to Proceed 1 <-- User input [System]: Starting Setup!... [System]: Would you like to use drive " C " for Installation [System]: Y/N? [Warning]: Setup Halted, Awaiting User Response [USER]: [Warning]: Setup Resumed [System ERROR]: Invalid Option, Restarting Setup... [System]: Would you like to use drive " C " for Installation [System]: Y/N? [Warning]: Setup Halted, Awaiting User Response y [USER]: y [Warning]: Setup Resumed [System]: Please type out the Directory you would like to install System Files, You do not need to include the slashed "" [System]: EXAMPLE " \Users* User *\Documents " [System]: Type "Cancel" to cancel and start over, Type "Back" to remove last directory
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I guess in powershell you are just pressing Enter without entering any number. You should check Scanner for presence of a valid input: [Scanner: No line found](https://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found) – Alex Sveshnikov Mar 05 '21 at 08:57
  • According to what you posted, when you run the code in PowerShell or [cmd](https://en.wikipedia.org/wiki/Cmd.exe), line 309 in file `Setup.java` is (indirectly) throwing an exception. I assume that you have written a class named `Setup`. Unfortunately, I could not find the code of that class in your question so I cannot help you further. – Abra Mar 05 '21 at 08:59
  • For what it's worth, when I run the code in your question, I do **not** get any errors at all. Assuming that you haven't already done so, I recommend that you read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Abra Mar 05 '21 at 09:06
  • @Abra So in the Setup class all it does is is open a new instance of the scanner class before asking if you want to install directories on the C drive... So the mainBody class has an instance of Scanner then closes it before it calls the setup class. So what it was doing before was not closing the scanner before it called the Setup class. So i added a close call for Scanner right before and now the issue occurs there instead – Brayden Anderson Mar 05 '21 at 09:14
  • If you want help debugging your code, then I can only help you if you post it (preferably in your question as text). As I wrote in my previous comment, try to post a [mcve]. (And please read that link since I believe it will help you to get an answer). – Abra Mar 05 '21 at 09:18
  • I have the full code on github, it lacks a lot of comments (Im working on fixing that)...https://github.com/braydenanderson2014/Solar – Brayden Anderson Mar 05 '21 at 09:18
  • Also @Abra, When i run the code through BlueJ ide it runs perfectly (That part anyway) when i try to run it in visual studio code or as a jar file through windows, it runs into that exception – Brayden Anderson Mar 05 '21 at 09:22
  • Don't close a `Scanner` that wraps `System.in` and don't create a new `Scanner` each time you want to accept input from the user. Create a global `Scanner` and access it whenever you want to accept user input. And also, I think you should read this: https://meta.stackoverflow.com/questions/376951/question-with-external-link-to-code – Abra Mar 05 '21 at 09:28
  • @Abra, for the minimal recreation I did. The code here that I posted in the question is smaller than the one in use in the program and it still doesn’t work. As for the closing of the scanner, I think I understand now. So thank you... I’m going to remove all close functions for the scanner and try again to see if the code works.. – Brayden Anderson Mar 05 '21 at 17:53

2 Answers2

0

Firstly do not close the scanner as mentioned in here.

Secondly it looks like BlueJ you are using is providing the required dependency magically if at all if you have not configured in that way. The rest of the tools are not doing this magic. In any case you should not be able proceed, that is the point.

Raghuveer
  • 2,859
  • 7
  • 34
  • 66
  • Thank you. I think I understand what’s happening now. I only closed it because anytime I use visual studio code it claims there is a resource leak there. I’ll remove all scan.close() calls and see if that works – Brayden Anderson Mar 05 '21 at 17:51
0

So Thank you Everyone for your help. The issue was the closing of the Scanner. I removed all closings and the created a class to handle all the Scanner stuff. Then i routed all input instances to the new class. So thank you!