0

I am creating a dynamic array of buttons, but i am getting Nullpointer Exception. Why? My sample code is:

 Button addAsFriend[];
 for( i=0;i<c.getCount();i++)
 {
    addAsFriend[i]=new Button(this);
 }

3 Answers 3

2

Because you didn´t initialized your array, try this:

Button addAsFriend[] = new Button[c.getCount()];
 for( i=0;i<c.getCount();i++)
 {
    addAsFriend[i] = new Button(this);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

how can i implement the click events for each button in this array
0

You aren't initializing the array itself, only the elements inside? See line 2:

Button addAsFriend[];
addAsFriend = new Button[c.GetCount()];
    for( i=0;i<c.getCount();i++)
        {
           addAsFriend[i]=new Button(this);
        }

Edit: Pretty silly of me to delete this.

Comments

0

The following code should help you out:

Button[] b = new Button[9];
b[0] = (Button) findViewById(R.id.button1);
b[1] = (Button) findViewById(R.id.button2);
b[2] = (Button) findViewById(R.id.button3);
b[3] = (Button) findViewById(R.id.button4);
b[4] = (Button) findViewById(R.id.button5);
b[5] = (Button) findViewById(R.id.button6);
b[6] = (Button) findViewById(R.id.button7);
b[7] = (Button) findViewById(R.id.button8);
b[8] = (Button) findViewById(R.id.button9);

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.