-1

i'm trying to make my scanner do some basic console interface, but it keeps returning NullPointerException no matter what i try. Here is the some of code:

public static void main(String[] args) {

    list nlist = new list();
    Scanner menu_input = null;
    [..] //a couple println here...
    opt = menu_input.nextInt(); //the error points to this line

                switch (opt) { ... }

why would it cause an error like this? I'm sorry if this is trivial, but this is my first real experience with Java.

Full text of the error in case it's useful:

Exception in thread "main" java.lang.NullPointerException
    at lab.newJava.main(newJava.java:75)
meta
  • 153
  • 12
  • 2
    Without initializing the scanner you are trying to get an input menu_input.nextInt(); gives you NPE – Shriram May 09 '17 at 09:32
  • 5
    You set `menu_input` to `null` - what do you expect that might achieve? – Ken Y-N May 09 '17 at 09:32
  • 1
    "_it's what eclipse suggested for some reason_" Eclipse don't know what you want, he just know what the compiler don't want, don't accept. – AxelH May 09 '17 at 09:41

1 Answers1

2

You forgot to initialize the Scanner:

Scanner menu_input = new Scanner(System.in);
Peter Bruins
  • 807
  • 9
  • 25
meskobalazs
  • 15,741
  • 2
  • 40
  • 63