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
instead of returning an array of strings in your topic method, I would return a List
so instead of
you could write
so now you can just use
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