samedi 5 septembre 2015

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?

Aucun commentaire:

Enregistrer un commentaire