Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in LogCat?

Tags:

android

Whenever I introduce the array, i get this error. I tried different array types...but no luck.

public class Classifytestclass2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         int test2[] = null;
        for(int i=0;i<50;i++){
               test2[0]=1;
        }
    }
}

edit: forgot to mention, the entire app crashes (force close.)

like image 590
user947659 Avatar asked Jul 24 '26 12:07

user947659


1 Answers

It's normal to get a NullPointerException because you didn't initialize the array(). You have to do something like this:

int test2[] = new int[50];

to initialize an array with 50 elements.

like image 84
user Avatar answered Jul 26 '26 05:07

user



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!