Это моя основная функция:
package com.ss.lms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
// if we close this once... it seems to be closed forever...
public static BufferedReader stdinBufReader = new BufferedReader(new InputStreamReader(System.in));
// our main function
public static void main(String[] args) {
MenuInterface.stdinBufReader = stdinBufReader;
while (MenuInterface.displayMenu()) { }
/*try { stdinBufReader.close(); }
catch (IOException e) { e.printStackTrace(); }*/
}
}
Я прошел класс MenuInterface
и выполнил текстовый поиск по слову «закрыть», и нигде там ничего не закрылось.
Здесь происходит ошибка:
package com.ss.lms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
// if we close this once... it seems to be closed forever...
public static BufferedReader stdinBufReader = new BufferedReader(new InputStreamReader(System.in));
// our main function
public static void main(String[] args) {
MenuInterface.stdinBufReader = stdinBufReader;
while (MenuInterface.displayMenu()) { }
/*try { stdinBufReader.close(); }
catch (IOException e) { e.printStackTrace(); }*/
}
}
Это трассировка стека:
Would you like to perform another operation? (Y/N): java.io.IOException: Stream closed
at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:169)
at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:335)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:297)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185)
at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
at com.ss.lms.MenuInterface.promptContinue(MenuInterface.java:108)
at com.ss.lms.MenuInterface.displayMenu(MenuInterface.java:47)
at com.ss.lms.Main.main(Main.java:14)
Exception in thread "main" java.lang.NullPointerException
at com.ss.lms.MenuInterface.isYesOrNo(MenuInterface.java:121)
at com.ss.lms.MenuInterface.promptContinue(MenuInterface.java:111)
at com.ss.lms.MenuInterface.displayMenu(MenuInterface.java:47)
at com.ss.lms.Main.main(Main.java:14)
Вот весь файл для MenuInterface.java
:
package com.ss.lms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import com.ss.lms.controllers.AuthorController;
public class MenuInterface {
// our list of entities
public static String[] entities = {"author", "book", "publisher"};
// maps a character to an action.
// e.g. 'c' => "create"
public static Map<String, String> crudOptions = new HashMap<String, String>();
// if we close this once... it seems to be closed forever...
public static BufferedReader stdinBufReader;
public static Boolean displayMenu() {
String selectedModel;
String selectedAction;
try {
selectedModel = promptModel();
selectedAction = promptCRUDAction();
switch (selectedModel) {
case "author":
switch (selectedAction) {
case "create":
AuthorController.create();
break;
case "read":
System.out.print("Enter author ID for retrieval: ");
Integer authorId = Integer.parseInt(stdinBufReader.readLine());
AuthorController.read(authorId);
break;
}
break;
}
} catch (IOException e1) {
e1.printStackTrace();
}
return promptContinue();
}
// Prompt the user for which entity they want to perform actions with.
public static String promptModel() throws IOException {
String selectedEntity;
int selectionNum = 0;
// prompt the user
System.out.println("1 - Author");
System.out.println("2 - Book");
System.out.println("3 - Publisher");
System.out.print("\nChoose an entity: ");
selectionNum = Integer.parseInt(stdinBufReader.readLine());
selectedEntity = entities[selectionNum - 1];
System.out.println("\nYou selected: " + selectedEntity + "\n");
return selectedEntity;
}
// Prompts the user for a character input, returns a string representation
// of the action they want to perform. E.g. "create", "read"...
public static String promptCRUDAction() throws IOException {
String selectedChar = null;
String selectedAction;
crudOptions.put("c", "create");
crudOptions.put("r", "read");
crudOptions.put("u", "update");
crudOptions.put("d", "destroy");
do {
System.out.println("(C)reate");
System.out.println("(R)ead");
System.out.println("(U)pdate");
System.out.println("(D)estroy");
System.out.print("\nChoose an action: ");
try { selectedChar = stdinBufReader.readLine(); }
catch (IOException e) { e.printStackTrace(); }
selectedAction = crudOptions.get(selectedChar);
if (selectedAction == null) {
System.out.println("\nInvalid Option. Try again.\n");
}
} while (selectedAction == null);
System.out.println("\nYou selected: " + selectedAction);
return selectedAction;
}
public static Boolean promptContinue() {
String userInput = null;
Boolean continueSelected = null;
do {
System.out.print("Would you like to perform another operation? (Y/N): ");
// keep getting weird errors saying the Stream is closed when it isn't
try { userInput = stdinBufReader.readLine().toLowerCase(); }
catch (IOException e) { e.printStackTrace(); }
if (!isYesOrNo(userInput)) {
System.out.println("Please enter either Y or N.");
}
continueSelected = userInput == "y" ? true : false;
} while (!isYesOrNo(userInput));
return continueSelected;
}
public static Boolean isYesOrNo(String userInput) {
return userInput.toLowerCase() == "y" || userInput.toLowerCase() == "n";
}
}