0

I have this enum class.

When I call the controller it gives me null

public enum Gender {

    MALE("Male", "M"),
    FEMALE("Female", "F"),
    OTHER("OTHER", "O");

    private String description;
    private String value;


    private Gender(String description, String value) {
        description = description;
        value = value;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return description;
    }
}


List<Gender> listGender = Arrays.asList(Gender.values());
for (Gender o : Gender.values()) {
   System.out.println(o.toString());
}

Answer null

Thanks for what you can help me

Translate with google translate

1 Answer 1

2

You made a mistake inside the enum constructor. Use the keyword this to assign value to object's attributes (an enum is an object).

private Gender(String description, String value) {
    this.description = description;
    this.value = value;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I was wrong to copy it and do not modify the constructor
You need to initialize attributes in the constructor Gender, using this keyword before the attribute's names. Without that the attribute stay null because the variables description and value referred to method's parameters and not to the class attributes.
this is all it needs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.