-2

I have to display category name on android textview with array items.here the total category size is 5.

I have wrote the below code:

for (int i = 0; i < sitesList.getName().size(); i++) {
name[] = (TextView) findViewById(R.id.title);
name[].setText(sitesList.getCategory().get(i).toUpperCase());

now i have to run the app which means am getting the last (5th) category name alone.But i wish to display the all(total=5) categories.

 for (int i = 0; i < sitesList.getName().size(); i++) {
 name[i] = (TextView) findViewById(R.id.title);
 name[i].setText(sitesList.getCategory().get(i).toUpperCase());

Now i have to run the app means my app is force closed.How can i resolve these error ??? please give me solution for these ???

My console window shows:

03-12 06:36:08.789: E/AndroidRuntime(886): Caused by: java.lang.NullPointerException
03-12 06:36:08.789: E/AndroidRuntime(886):  at  com.xmlparsing.MainActivity.onCreate(MainActivity.java:72)
03-12 06:36:08.789: E/AndroidRuntime(886):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

In my code 72th line is :

    name[i] = (TextView) findViewById(R.id.title);

But i have to create the textview programmatically which means its worked well and displayed all(total=5) categories well.

user2098063
  • 85
  • 1
  • 1
  • 7

1 Answers1

0

This may not be the answer, but it may help get you in the right direction, or at least get you to clarify your intentions.

Based on the NullPointerException in the stack, and the only thing that could be causing such an exception on that line is name[i], I'm going to guess that you haven't created the name property correctly.

TextView [] name = new TextView[5];

Even still, there seems to be a better way to do what you're trying to achieve. In your code, you're trying to access and set the same TextView's text to a new value in each iteration of the loop, so that same TextView's text will always be the last sitesList.getCategory().get(i) value.

Phix
  • 9,364
  • 4
  • 35
  • 62