1

Can someone explain to me why when I try to use the variable custemp,emptemp,admdtemp after the if statement I get null and how to fix it?

case 2:// login a user but do some input validation
            System.out.println("Enter login Username: ");
            username =s.nextLine();
            
            if (userDao.findByUsername(username).getRole().equals("Customer")){
            custemp=userDao.findByUsername(username);
            
            }else if
(userDao.findByUsername(username).getRole().equals("Employee")){
                emptemp=userDao.findByUsername(username);
                
            }else if (userDao.findByUsername(username).getRole().equals("Admin")){
                    admtemp=userDao.findByUsername(username);
                    
                }
        
            //System.out.println(custemp);
            boolean k= aService.usernameAuthentication(username);
            if(k==false) {
                do {
                System.out.println("Username does not exist enter in a valid username");
                System.out.println("Enter username: ");
                username=s.nextLine();
                k=aService.usernameAuthentication(username);
                
               
                }while(!k);
            }
            System.out.println("Enter login Password: ");
            password=s.nextLine();
            boolean m= aService.passwordAuthentication(username,password);
            if(m==false) {
                do {
                System.out.println("Password is not associated with user");
                System.out.println("Enter password: ");
                password=s.nextLine();
                m=aService.passwordAuthentication(username,password);
                
               
                }while(!m);
            }
            
            boolean c=userService.login(username, password);
            while(c) {
                
                if(custemp.getRole().toString()=="Customer") {
                    
                    while(!gobackmainMenu) {
                    printMenuCusAccM();
                    int choice2= getInputCusAccM();
                    operonCusAcc(choice2);
                    }
                }
                if(emptemp.getRole().toString()=="Employee") {
                    printMenuEmpAccM();
                    while(!gobackmainMenu) {
                    int choice3= getInputEmpAccM();
                    operonEmpAcc(choice3);
                    
                    }
                }
                if(admtemp.getRole().toString()=="Admin") {
                    printMenuAdmAccM();
                    while(!gobackmainMenu) {
                    int choice4= getInputAdmAccM();
                    
                    }
                }
            }
            
            break;
mnewm9
  • 41
  • 2
  • 3
    Not what you asked, but you'll want to read [How do I compare strings in Java?](https://stackoverflow.com/q/513832) – Dawood ibn Kareem Aug 31 '20 at 02:47
  • Your code is not making sense. What do you want e.g. `emptemp` to be if the user is not an Employee? – Andreas Aug 31 '20 at 02:48
  • @Andreas A user is either employee, admin, or customer – mnewm9 Aug 31 '20 at 02:51
  • *FYI:* 1) Don't call `userDao.findByUsername(username)` multiple times. Call it once and assign to a local variable, then use the variable multiple times. --- 2) Do the `usernameAuthentication()` and `passwordAuthentication()` checks *before* checking the role of the user, otherwise `findByUsername()` will return null and cause a `NullPointerException`. --- 3) `boolean k = authenticate(); if (k == false) { do { ... k = authenticate(); } while (! k); }` is better written as `while (! authenticate()) { ... }` – Andreas Aug 31 '20 at 02:53
  • 1
    @mnewm9 That didn't answer my question. What do you want the value of variable `emptemp` to be when the user is **not** an Employee. I mean, obviously you want `custemp` to be the user when the user is a Customer, so what should `emptemp` be in that scenario? Null? If so, how did you expect `if(emptemp.getRole().toString()=="Employee")` to not throw a `NullPointerException`? Or perhaps you meant to write `if (emptemp != null)`, since that would then only be true if the user *is* an Employee. – Andreas Aug 31 '20 at 02:56

1 Answers1

0

You are only ever setting one of custemp, emptemp, and admdtemp. When you get to the while block at the bottom, all three of them are being referenced. Since only one is ever actually populated, the other two are always null (assuming they are not set elsewhere in code not in your original question).

To fix it, you would need to either only reference the one you set, or to make sure the others were at least initialized to something present, but not necessarily usable for their roles.

Gryphon
  • 384
  • 1
  • 9
  • This only part of my code. These values are referenced through out. Even after setting them I get a null pointer exception `if(custemp.getRole().toString()=="Customer") ` at this line. these are private static variables in my class. Without the if statements and just using one `temp` variable it creates the user and stores it to be used elsewhere in the code. So my question is how to get those variables to have a value beyond the if statement? – mnewm9 Aug 31 '20 at 02:44
  • They will have values beyond the ```if``` statement, if they have been set. But if they are coming back as null, they have not been set (or their values have been set back to null). It's also possible that they have been set, but ```getRole()``` is returning null. Unfortunately, I can't help with code I can't see. – Gryphon Aug 31 '20 at 02:53