-3

I don't understand why in my code I get a :

null reference object

when i try to click on my buttons.

buttonMenuOption = findViewById(R.id.buttonOption);
Button[] buttonsOption = new Button[nbObjects];
buttonsOption[0] = findViewById(R.id.buttonOption0);
buttonsOption[1] = findViewById(R.id.buttonOption1);
buttonMenuOption.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        setContentView(R.layout.graph_option);
            for (int i = 0; i < nbObjects; i++) {
                final int j = i;
                    buttonsOption[i].setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            //Do stuff
                          }
                    });
             }
     });

To resume my code, there is an option button in my main activity, when I press it, it changes the layout then there are multiples buttons to activate a color picker.

Why is my buttonsOption[0] a null reference?

Update

buttonMenuOption = findViewById(R.id.buttonOption)
buttonMenuOption.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
     setContentView(R.layout.graph_option);
     Button[] buttonsOption = new Button[nbObjects];
        for (int i = 0; i < nbObjects; i++) {
            final int j = i;
            buttonsOption[0] = findViewById(R.id.buttonOption0);
            buttonsOption[1] = findViewById(R.id.buttonOption1);
            buttonsOption[i].setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        //Do stuff
                      }
                });
         }
 });
BeGreen
  • 765
  • 1
  • 13
  • 39

3 Answers3

1

problem occurred from

buttonsOption[0] = findViewById(R.id.buttonOption0);
buttonsOption[1] = findViewById(R.id.buttonOption1);

which are declared before related layout initialize.

so you may have to declare button reference after setContentView(R.layout.graph_option); like below

    buttonMenuOption = findViewById(R.id.buttonOption);
 final Button[] buttonsOption = new Button[nbObjects];

buttonMenuOption.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        setContentView(R.layout.graph_option);
         buttonsOption[0] = findViewById(R.id.buttonOption0);//change here
         buttonsOption[1] = findViewById(R.id.buttonOption1);// change here
            for (int i = 0; i < nbObjects; i++) {
                final int j = i;
                    buttonsOption[i].setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            //Do stuff
                          }
                    });
             }
     });
Omkar
  • 3,040
  • 1
  • 22
  • 42
  • Wow...I was talking with someone while typing... You should add more content to your answer. Highlight the problem and what you have done to make it work. _like below_ is a too broad ;) – AxelH Dec 05 '17 at 12:10
  • @AxelH ohh.. ok done answer edited! – Omkar Dec 05 '17 at 12:20
  • 1
    This is always better to post an explanation with the code (the comments are a nice touch by the way ;) ) It will be easier to understand and of course, it gain in interest. – AxelH Dec 05 '17 at 12:22
  • @AxelH yes your right, next time I'll take it in mind! – Omkar Dec 05 '17 at 12:24
0

You are using findViewById before you setContentView(), there is no view to find other view in. You need to first set your XML as your activity's view then you can call findViewById

Or you can do things a bit differently, like that

buttonMenuOption = findViewById(R.id.buttonOption);
Button[] buttonsOption = new Button[nbObjects];
final View newContentView = LayoutInflater.from(this).inflate(R.layout.graph_option, null);
buttonsOption[0] = newContentView.findViewById(R.id.buttonOption0);
buttonsOption[1] = newContentView.findViewById(R.id.buttonOption1);
buttonMenuOption.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        setContentView(newContentView);
            for (int i = 0; i < nbObjects; i++) {
                final int j = i;
                    buttonsOption[i].setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            //Do stuff
                          }
                    });
             }
     });
elmorabea
  • 3,243
  • 1
  • 14
  • 20
-1

Is it because you have declared an array of buttons but not declared any button for the index of the array?

If buttonsOption[0] is null, I would declare the array and then the button.

Button[] buttonsOption = new Button[nbObjects];

for(int i=0; i<nbObjects;i++){
    buttonOption[i] = new Button();
}
Richardweber32
  • 168
  • 2
  • 16