-2

I had made 5 textviews, textview1 to textview5. Then I created a textview array like this:-

TextView tvArr[] = new TextView[] { textview1, textview2, textview3, textview4, textview5 };

When I am using the setText method, it is throwing NullPointerException -

tvArr[3].setText("Done");

Please help me find out what I've missed out, and thanks in advance...

Edit: I want to find out how the set text method is throwing NullPointerException, I am not asking to troubleshoot my code...

Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
Pd Unique
  • 26
  • 6
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Mar 28 '19 at 12:21
  • I am not understanding what here is throwing NullPointerException ? – Pd Unique Mar 28 '19 at 12:23
  • what is null point error ? @PdUnique – Ashish Mar 28 '19 at 12:25
  • 1
    You have to put a object in the array slots. You are trying to set something that does not exist. – Mr00Anderson Mar 28 '19 at 12:27
  • 1
    Are you initialising all the textview before adding them to the array? – iCantC Mar 28 '19 at 12:28
  • 1
    _"I had made 5 textviews, textview1 to textview5."_ How exactly? You could edit the question and add your code. Just typing `TextView textview1;` won't be enough. The object will be `null`. You probably want to use `findViewById()` to find the TextViews if you defined them in the layout XML file. And `findViewById()` may return `null` if you made a mistake somewhere, like calling it with the wrong id or calling it in a wrong place like before `setContentView()` for example. – Markus Kauppinen Mar 28 '19 at 12:28
  • post your entire code, this will help us to help you? – iCantC Mar 28 '19 at 12:30
  • Check this out too first [Java tutorial by Oracle -Arrays](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) – Mr00Anderson Mar 28 '19 at 12:30
  • I have initialised the TextViews – Pd Unique Mar 28 '19 at 12:33
  • _"I have initialised the TextViews"_ How? – Markus Kauppinen Mar 28 '19 at 12:58

1 Answers1

1

After declaring the text views array, you have to initialize the items in it. Use either

tvArr[3] = new TextView(this);
tvArr[3].setText("Done");
parentLayout.addView(tvArr[3]);

or

tvArr[3] = (TextView)findViewById(R.id.txt_view);
tvArr[3].setText("Done");
Maria
  • 369
  • 2
  • 7
  • Adding objects to an array should be like TextView[] tvArr = new TextView[5]; tvArr[0] = textView1; tvArr[1] = textView2; etc. Then you can use it as tvArr[0].setText("Done"); – Maria Mar 28 '19 at 12:47
  • Those are not the only possible ways. For example the Oracle Java documentation page linked in the question comments gives an example of a similar way (albeit with primitives) as the OP used: `int[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};`. It should be perfectly valid. Of course assuming that the objects added to the array are non-null. – Markus Kauppinen Mar 28 '19 at 13:27