-3

I've tried numerous ways to fix this but I'm just not having any luck. I've heard you get the error by making a null.equals(null) but I set the value before it got inputted. I'm just really lost, can someone explain this? Here is my code:

public static void main(String[] args) {
       String MenuChoice = "";
       String MenuOption = "nothing";
    System.out.println("Welcome to my Fashion!");
       System.out.println("\n");
       System.out.println("Are You Signing In As An <Employee> Or <Admin>?");
       MenuChoice = inputData.next();
       MenuOption = MenuChoice;

       if(MenuOption.equals("Employee")) {
           Passwords.MainPassword();
       }
      if(MenuOption.equals("Admin"));
      {
          Passwords.AdminPassword();
      }
}

My code is not a duplicate because I am involving strings, not variables. Also, only asking about .equals.

  • 2
    is there is a chance inputdata.next() is returning a null? – amritanshu Apr 11 '18 at 05:03
  • MenuChoice = inputData.next(); debug this line – Atif AbbAsi Apr 11 '18 at 05:04
  • 1
    What do you mean by debug? – Jay Sorensen Apr 11 '18 at 05:05
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –  Apr 11 '18 at 05:05
  • better to compare constants againts variables, `"Employee".equals(MenuOption)` –  Apr 11 '18 at 05:05
  • Your code doesn't show what `inputData` is, it could be null or return null. Also the StackTrace would contain useful information where the exception occurs. –  Apr 11 '18 at 05:07
  • Based on you comments and edit: Yes your question is a duplicate, since you only mention that this code throws a `NullPointerException` and you make the assumption that it's caused by `.equals` which you disprove by your comments in the answer below. So the first step is to find out where the exception occurs, this is possible with a full StackTrace (Read: [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788/8097737)) –  Apr 11 '18 at 05:41

1 Answers1

0

There may be chance here inputData.next() will return null so in that case it will throw NullPointerException

My suggestion is always use CONSTANT.equals(str). by Using this way if str is null than it will check in equals method and your code will not throw NullPointerException

so change your code to -> "Admin".equals(MenuOption) -> this will resolve your problem.

Meet Patel
  • 482
  • 4
  • 12