This one is a bit weird. I have read through many threads about NoSuchElementException: No line found and it typically seems to be related to a scanner object being closed or not having anymore lines to read. However, my problem is that I only get this error when I stop my program in Eclipse by clicking on the red square. I can start my program and then click stop as soon as it gets to the first user prompt, not enter anything, and once I click stop, it gives me the error. The program works fine until I hit the stop button, it's the weirdest thing. This never used to be a problem. I've been working on this project for several weeks and never once had this happen until today. I did update from Eclipse 2020-06 to Eclipse 2020-12 because I was trying to get STS 4 working as a plugin (a whole nother problem), so I'm wondering if it's something to do with the new version of Eclipse. I did not notice this start happening until after updating today. I have double, triple, quadruple checked my code and tried commenting out different parts to see if it was a certain loop or something that was causing it, but the errors persists no matter which sections I comment out. I have checked that my scanner is not being closed early, I do not have multiple instances of scanner being created, the lines are being read properly, etc. The entire code is on my github at:
https://github.com/ksinelli/OnlineBanking/tree/master/OnlineBanking
It starts at LoginScreen class and the only other relevant code would be in DatabaseConnection class and MyScanner class, considering I don't even need to advance far enough in my program to get to the other classes for it to give this exception.
For those who don't feel like viewing the github, the LoginScreen class is this:
package HomePage;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import Customer.Customer;
import Customer.CustomerDashboard;
import Utility.MyScanner;
import Utility.DatabaseConnection;
public class LoginScreen {
private static ResultSet resultSet;
private static PreparedStatement stmt;
public static void main(String[] args) {
try {
DatabaseConnection.getDbConnection();
MyScanner.openScanner();
System.out.println("Hello, and welcome to KAS Financial!\n");
startup();
}
catch (Exception e){
e.printStackTrace();
}
}
public static void startup() {
Customer customer = new Customer();
boolean isRunning = true;
while (isRunning == true) {
System.out.println("Type \"Sign in\" to access your account.");
System.out.println("Type \"Create account\" to create a new account.");
System.out.println("Type \"Reset password\" to change your password.\n");
System.out.println("Type \"Home\" at any time to return to this menu.\n");
System.out.println("Pro tip: capitalization doesn't matter when typing your choice :)");
String choice = MyScanner.getInputToLower();
switch (choice) {
case "sign in":
isRunning = false;
signIn(customer);
break;
case "create account":
isRunning = false;
createAccount(customer);
break;
case "reset password":
isRunning = false;
resetPassword(customer);
break;
default:
System.out.println("I'm sorry, I didn't understand that. Please try again.\n");
}
}
}
public static void signIn(Customer customer) {
customer.setUsername(getUsernameFromDb(customer));
customer = getPasswordFromDb(customer);
CustomerDashboard.dashboardMenu(customer);
}
public static void resetPassword(Customer customer) {
customer.setUsername(getUsernameFromDb(customer));
customer = getPasswordFromDb(customer);
updatePassword(customer);
}
//retrieve entered username from database and if it does not exist, redirect to home page.
public static String getUsernameFromDb(Customer customer) {
System.out.println("Please enter your username.");
String input = MyScanner.getInputToLower();
try {
stmt = DatabaseConnection.prepareStatement("select username from customer where username = ?");
stmt.setString(1, input);
resultSet = stmt.executeQuery();
if (resultSet.next() == false) {
System.out.println("That username does not exist in our system.\n");
startup();
}
}
catch (SQLException e) {
e.printStackTrace();
}
return input;
}
//retrieve database password and compare to entered password. If they do not match, return to home page.
public static Customer getPasswordFromDb(Customer customer) {
System.out.println("Please enter your password.");
String password = MyScanner.getInput();
String dbPassword;
try {
stmt = DatabaseConnection.prepareStatement("select password from customer where username = ?");
stmt.setString(1, customer.getUsername());
resultSet = stmt.executeQuery();
resultSet.next();
dbPassword = resultSet.getString("password");
if (!password.equals(dbPassword)) {
System.out.println("The password you entered does not match our records. Please try again.\n");
startup();
}
customer.pullProfileFromDatabase(customer);
}
catch (SQLException e) {
e.printStackTrace();
}
return customer;
}
//Update SQL database with new user-supplied password
public static void updatePassword(Customer customer) {
System.out.println("Please enter a new password.");
String newPassword = MyScanner.getInput();
try {
stmt = DatabaseConnection.prepareStatement("Update customer set password = ? where username = ?");
stmt.setString(1, newPassword);
stmt.setString(2, customer.getUsername());
stmt.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
System.out.println("Your password has been updated. Please login again with your new credentials.\n");
signIn(customer);
}
//create new customer profile, assign it to customer object, and insert it into database
public static void createAccount(Customer customer) {
boolean usernameIsValid = false;
while (usernameIsValid == false) {
System.out.println("Please enter a username.");
customer.setUsername(MyScanner.getInputToLower());
try {
stmt = DatabaseConnection.prepareStatement("select username from customer where username = ?");
stmt.setString(1, customer.getUsername());
resultSet = stmt.executeQuery();
if (resultSet.next()) {
System.out.println("That username already exists.\n");
}
else {
usernameIsValid = true;
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("Please create a password for your account.");
customer.setPassword(MyScanner.getInput());
try {
stmt = DatabaseConnection.prepareStatement("Insert into customer (username, password) values (?,?)");
stmt.setString(1, customer.getUsername());
stmt.setString(2, customer.getPassword());
stmt.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
customer.createOrUpdateProfile(customer);
customer.pushProfileToDatabase(customer);
System.out.println("Your account has been created. Please login using your new credentials.\n");
signIn(customer);
}
}
The DatabaseConnection class is this
package Utility;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DatabaseConnection {
static Connection conn;
public static void getDbConnection() {
try {
conn = DriverManager.getConnection(connection deets here);
}
catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeDbConnection() {
try {
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public static PreparedStatement prepareStatement(String SQL) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(SQL);
return stmt;
}
}
And the MyScanner class is this
package Utility;
import java.util.Scanner;
import HomePage.LoginScreen;
public class MyScanner {
public static Scanner scan;
public static void openScanner() {
scan = new Scanner(System.in);
}
public static void closeScanner() {
scan.close();
}
public static String getInput() {
String input = scan.nextLine();
if (input.toLowerCase().equals("home")) {
LoginScreen.startup();
}
return input;
}
public static String getInputToLower() {
String input = scan.nextLine().toLowerCase();
if (input.equals("home")) {
LoginScreen.startup();
}
return input;
}
public static int getNumber() {
int input = scan.nextInt();
scan.nextLine();
return input;
}
}
Any ideas?
Can someone open this back up please? I don't see how this is a duplicate. I've searched all over the internet and I've yet to come across a thread about getting an exception - any exception at all - only after manually stopping the program.
Update: This appears to be a problem with Eclipse 2020-12 or perhaps JRE 14. I reinstalled Eclipse 2020-06, which is what I originally was working with until this error occurred. I copied and pasted the code into a new project and everything ran fine - no errors after stopping the program. I noticed in the Run Configuration that the JRE was set to Java SE 11. However, in the 2020-12 version, it is set to Java SE 14. I'm not sure how to properly change it, so I don't know if that is exactly what's causing the error (as I said I'm still a newbie at this stuff). But thanks for closing my question and telling me it was a duplicate when you probably didn't even read the whole thing.