views:

50

answers:

2

Hi guys,

Im trying to display this listview, but I keep getting a:

07-17 21:14:22.233: ERROR/AndroidRuntime(349): java.lang.NullPointerException

I think I know what the problem is but I dont know how to fix it.

I have a String array which I will be using to be displayed in my list

parser topicparser = new parser();
  final String[] TOPICS = topicparser.topic("http://www.test.com");

Dont worry the parser works great. the only problem I see with it is that the parser doesnt know how many strings the website will have so I am setting the String array in the parser to be very big:

String substr[] = new String[250];

but I know that mostly there are only like 11 to 13 values that I will be storing in that array, the problem I think is when i do this:

setListAdapter(new ArrayAdapter<String>(this, R.layout.topics, TOPICS));

  ListView lv = getListView();

since the String array is set to have up to 250 values in it and I only actually store say 12 then there are a null entries so when I run the code it shows me my list view, but when I get to the bottom of the view it force closes and the log tells me that there was nullpointer. I can tell how many entries I collected while I am parsing, but how do I tell my listview to only show say 12 items in the list so there wont be any null entries. or is there any other solution for my problem.

Thank you,

@Cristian C.

I tried doing what you said, but now I am getting a:

java.lang.ClassCastException: java.util.Arrays$ArrayList

this is what I did:

String[] TOPICS = topicparser.topic("test.com"); 
ArrayList<String> niceArray = (ArrayList<String>) Arrays.asList(TOPICS); // eclipse told me I had to add the cast 
setListAdapter(new ArrayAdapter<String>(this, R.layout.topics, niceArray)); 

any ideas ? Thanks

+3  A: 

Instead of using a normal array, use a ArrayList.

To convert the normal array to ArrayList, you can do something like:

ArrayList<String> niceArray = new ArrayList(Arrays.asList(normalArray));

Those classes are included in the java.util package.

Cristian
and Can i pass that arraylist to the listview adapter like so:ArrayList<String> niceArray = Arrays.asList(TOPICS); // topics was // my string arraysetListAdapter(new ArrayAdapter<String>(this, R.layout.topics, niceArray));
jramirez
Please see edit of question above
jramirez
I tried the changes you said now I dont get a java.lang.ClassCastException: java.util.Arrays$ArrayListbut I sill get java.lang.NullPointerException
jramirez
+1  A: 

instead of returning an array of strings in your topic method, I would return a List

so instead of

String substr[] = new String[250]

you could write

ArrayList<String> substr = new ArrayList<String>();

so now you can just use

substr.add("whatever string");

the arraylist will only be as big as the items that you add, so you won't have to worry about nullpointer exceptions

and since the topic method now returns an ArrayList, the last part can be rewritten as

niceArray = topicparser.topic("test.com");
setListAdapter(new ArrayAdapter<String>(this, R.layout.topics, niceArray));
mportiz08
I am going to try this next thank you
jramirez
Thank you very much that solved my problem !
jramirez