0

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();
    }
}

Code+Debug view

Tom
  • 14,120
  • 16
  • 41
  • 47
J.Mead
  • 13
  • 5
  • The linked answer(s) explain why your code fails. You don't get the NoSuchElementException, because you use `scanner.hasNextLine()`, but that will always return `false` after re-opening the Scanner – Tom Nov 23 '20 at 14:02

2 Answers2

1

Could it be due to you closing your scanner and at the same time closing System.in? I cant comment so I can only post an answer. sorry if it doesnt work.

YHStan
  • 64
  • 1
  • 9
  • You shouldn't leave scanners open, it can cause all kinds of problems i do believe your along the right lines as i do think it is my scanner causing the problem but i can't put a finger as to why as it should be creating a new scanner each time its called but i belive when i open scanner with a system line, I'm closing the scanner but the systemline is remaining open so when i call the next line its essentially skipping the new input and going to line 2 of the systemline not line 1 – J.Mead Nov 23 '20 at 14:05
  • @J.Mead "systemline is remaining open" thats incorrect, see the linked duplicate – Tom Nov 23 '20 at 14:07
  • Based onTom's comment, since the error is in the constant reopening of the scanner, you could instead open the scanner once in your main method, and pass it as a parameter to your functions if needed and close it only when your method is over completely (at the end of main) – YHStan Nov 23 '20 at 14:13
0

Menus should be done within while loops.

You could use a structure similar to this :

public static int menu()
{
  int choiceMenu = 0;
  boolean validChoice = false;
  
while (validChoice == false)
  {
    System.out.println ("Select file to import :");
    System.out.println ("1. Part 1.");
    System.out.println ("2. Part 2.");
    System.out.println ("3. Part 3.");
    Scanner waitingForChoice = new Scanner(System.in);
    
    try
    {      
      choiceMenu = waitingForChoice.nextInt();
      if (choiceMenu < 4 && choiceMenu > 0)
      {
        waitingForChoice.close();
        validChoice = true;
        return choiceMenu;
      }
        else
        {
          validChoice = false;
          System.out.println("Entry invalid. Enter 1, 2, or 3.");
        }  
    } 
      catch (InputMismatchException e)
      {
        validChoice = false;
        System.out.println("Must be a number. Enter 1, 2, ou 3.");
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
  } 
  return choiceMenu;
}

I validate the choice is the right type, and within the right range of numbers. The while loop will run as long as inputs are invalid. When the user has selected a valid option, the while loop exits and returns the menu choice.

Jay
  • 15
  • 6