0

I've this class:

public class MenuUpElement {

    Class<?> classe;
    String label;
    int viewId;

    public MenuUpElement(int viewId, String label, Class<?> classe) {
        viewId = this.viewId;
        classe = this.classe;
        label = this.label;
    }
}

Then I have a static class StaticClass with this declaration:

public static final MenuUpElement[] menuUpElements = new MenuUpElement[]{
    new MenuUpElement(12, "Main", MainActivity.class)
    , new MenuUpElement(13, "Second", SecondActivity.class)
    };

If I loop through StaticClass.menuUpElements in another class, I found two elements (correct) but all null (wrong):

menuUpElements[0].classe = null
menuUpElements[0].viewId= 0
menuUpElements[0].label= null

menuUpElements[1].classe = null
menuUpElements[1].viewId= 0
menuUpElements[1].label= null

Why?

2 Answers 2

6

The assignments in your constructor are backwards

public MenuUpElement(int viewId, String label, Class<?> classe) {
    viewId = this.viewId;
    classe = this.classe;
    label = this.label;
}

Consider

public MenuUpElement(int viewId, String label, Class<?> classe) {
    this.viewId = viewId;
    this.classe = classe;
    this.label  = label;
}
Sign up to request clarification or add additional context in comments.

1 Comment

AH AH AH OH MY GOD DUMB DUMB DUMB!!! I did it tons of times :P Maybe, yesterday I was too tired ;)
1

The constructor of MenuUpElement is wrong, you're setting the parameters with the values of the fields. It should be the other way around:

public MenuUpElement(int viewId, String label, Class<?> classe) {
    this.viewId = viewId;
    this.classe = classe;
    this.label = label;
}

Comments

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.