Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at System.Main.main(Main.java:14) IDE: Eclipse IDE 2021-06 (4.20.0) Java Version: JDK 17 Related Code: package System; import java.util.Scanner; public class CmdRegister implements Command { public CmdRegister() {} public String[] getParamList() { Scanner in = new Scanner(System.in); String[] paramList = new String[2]; System.out.print("Username:"); paramList[0] = in.nextLine(); System.out.print("Password:"); paramList[1] = in.nextLine(); in.close(); return paramList; } public void execute(Role role, String[] paramList) { if(paramList!=null&&role.canRegister()) { ManagementPortal managementPortal = ManagementPortal.getInstance(); managementPortal.register(paramList[0], paramList[1]); } else if(paramList==null) {} else { System.out.print("Logged in user cannot register.\n"); } } } package System; import java.util.Scanner; public class Main { public static void main(String[] args) { ManagementPortal managementPortal = ManagementPortal.getInstance(); boolean onGoing = true; Scanner in = new Scanner(System.in); while (onGoing) { managementPortal.printOpeningMessage(); System.out.println("Please input command:"); String command = in.nextLine(); String[] cmdList = command.split(" "); if (cmdList[0].equals("Reg")) { CmdRegister cmd = new CmdRegister(); String[] paramList = cmd.getParamList(); cmd.execute(managementPortal.getCurrentUserRole(), paramList); } else if(cmdList[0].equals("Info")) { CmdCheckUserInfo cmd = new CmdCheckUserInfo(); String[] paramList = cmd.getParamList(); cmd.execute(managementPortal.getCurrentUserRole(), paramList); } else if(cmdList[0].equals("ChngPlan")) { CmdChangeUserPlan cmd = new CmdChangeUserPlan(); String[] paramList = cmd.getParamList(); cmd.execute(managementPortal.getCurrentUserRole(), paramList); } else if(cmdList[0].equals("PayHis")) { CmdCheckPersonalPaymentHistory cmd = new CmdCheckPersonalPaymentHistory(); String[] paramList = cmd.getParamList(); cmd.execute(managementPortal.getCurrentUserRole(), paramList); } else if(cmdList[0].equals("SignOut")) { CmdSignOut cmd = new CmdSignOut(); String[] paramList = cmd.getParamList(); cmd.execute(managementPortal.getCurrentUserRole(), paramList); } else if(cmdList[0].equals("TopUp")) { CmdTopUp cmd = new CmdTopUp(); String[] paramList = cmd.getParamList(); cmd.execute(managementPortal.getCurrentUserRole(), paramList); } else if(cmdList[0].equals("ResInd")) { CmdReserveIndividualSpace cmd = new CmdReserveIndividualSpace(); String[] paramList = cmd.getParamList(); cmd.execute(managementPortal.getCurrentUserRole(), paramList); } else if(cmdList[0].equals("CanRes")) { CmdCancelReserveRecord cmd = new CmdCancelReserveRecord(); String[] paramList = cmd.getParamList(); cmd.execute(managementPortal.getCurrentUserRole(), paramList); } else if(cmdList[0].equals("BuyPac")) { CmdBuyPackage cmd = new CmdBuyPackage(); String[] paramList = cmd.getParamList(); cmd.execute(managementPortal.getCurrentUserRole(), paramList); } else if(cmdList[0].equals("Exit")) { onGoing=false; } else { System.out.print("Sorry your command abbreviation cannot be recognized.\n" + "Please make sure it's exactly the same with the abbreviation name.\n"); } } System.out.println("Exit sucessfully!"); } }
Solved. Reason: no need to close the scanner inside a called function. Otherwise the scanner in main function will be eliminated.