I'm trying to get a menu option styled to-do list that allows a user to select what they wish to do. When I run the code it allows me to input once, then when it returns and I want it to wait for userInput a second time for them to navigate again, it throws a NullPointerException. I've tried various ways to solve this.
public class ToDoListApplication {
Scanner scanner;
String input;
public ToDoListApplication() {
System.out.println("Starting Application\nType 'Help' for Options\nPlease Select an Option..");
scanner = null;
menu();
}
public void menu() {
input = null;
if (input == null) {
input = userInput();
}
methodSelector(input);
}
public String userInput() {
scanner = new Scanner(System.in);
if (scanner.hasNextLine()) {
input = scanner.nextLine();
}
scanner.close();
return input;
}
public void methodSelector(String userInput) {
if (userInput.equalsIgnoreCase("help")) {
helpMethod();
} else {
System.out.println("unknown string");
}
}
public void helpMethod() {
System.out.println("Command 'Add': Add To-do item to list.\nType 'Remove': Remove Item from List.\nType 'Count': See how many items in list.\nType 'Find': Locate specific item by name.\nType 'Print': See all items in list.");
menu();
}
}