简体   繁体   中英

Java - If statement scope variable

I have a problem with the scope when I'm using the if statement ... I can't get the value from inside if else . The object has a null value in the message variable.

Class

public class UserMessage{
    String message;
    //Getters and Setters
}

Method

@RequestMapping(value = "/userMessage")
public @ResponseBody UserMessage getMessage(Principal principal){
    String username = principal.getName();

    UserMessage userMessage = new UserMessage();

    if(username.equals("admin")){
        //Set value to "message" variable
        userMessage.setMessage("Hi admin!")
    }

    else 
    if (username.equals("seller")) {
        //Set value to "message" variable
        userMessage.setMessage("Hi seller!")
    }

    else 
    if (username.equals("customer")) {
        //Set value to "message" variable
        userMessage.setMessage("Hi customer!")
    } 

    // This object has a null value in the message variable.
    return userMessage;

}

How can I resolve this problem?

Your formatting is masking the problem a wee bit: there is the case in which username does not exactly equal any of these strings:

  • "admin"
  • "seller"
  • "customer"

The reason is simple: you don't have an else condition with the long chain of if -statements.

if (username.equals("admin")) {
    //Set value to "message" variable
    userMessage.setMessage("Hi admin!")
} else if (username.equals("seller")) {
    //Set value to "message" variable
    userMessage.setMessage("Hi seller!")
} else if (username.equals("customer")) {
    //Set value to "message" variable
    userMessage.setMessage("Hi customer!")
}

If you absolutely need a value for it, then set it to some sensible default; otherwise, consider making your equals statements case insensitive instead, if the casing of username could be slightly off ( "Admin" instead of "admin" , etc).

// Also, flip the order of your equals so you don't run into silly NPEs
if("admin".equalsIgnoreCase(username))

It'd be beneficial for you to log the value of username out in at least DEBUG or TRACE until you can figure out why the value doesn't come in a form that you anticipate.

Try adding a default else clause to troubleshoot the issue:

else {
    userMessage.setMessage("Hi " + username);
} 

Here is a simple test application based on your code. The output is:

name: admin message: Hi admin!
name: xyzzy message: null

As indicated in Makoto's answer , if you run your if structure with one of the username values that cause a setMessage call, the message is set, and there is no problem retrieving it from outside the if . If you run the if structure with any other username value, you do not call setMessage, and the message retains its initial, null, value.

public class Test {
  public static void main(String[] main) {
    testit("admin");
    testit("xyzzy");
  }

  private static void testit(String name) {
    UserMessage message = getMessage(name);
    System.out.println("name: " + name + " message: " + message.getMessage());
  }

  public static UserMessage getMessage(String username) {
    UserMessage userMessage = new UserMessage();

    if (username.equals("admin")) {
      // Set value to "message" variable
      userMessage.setMessage("Hi admin!");
    }

    else if (username.equals("seller")) {
      // Set value to "message" variable
      userMessage.setMessage("Hi seller!");
    }

    else if (username.equals("customer")) {
      // Set value to "message" variable
      userMessage.setMessage("Hi customer!");
    }

    // This object has a null value in the message variable.
    return userMessage;

  }
}

class UserMessage {
  String message;

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM