So I have an activity in which it gets an XML file through a web service. It reads the XML and stores the relevant texts from that XML to a List<String>
object called xmlText. After retreiving all the text, I am starting another activity in which it contains a Linear layout, inside the linear layout is a ListView. I am passing the contents of xmlText by populating an ArrayList<String>
object called xmlInString. I then pass xmlInString through an intent.
I want to populate the listView to display the text from the xml file. In the activity I just started, I am populating a List<String>
object in which it will contain the elements from xmlInString. I then instantiate the arrayadapter in which the listview calls the method, setAdapter() and I pass in the arrayadapter object.
But everytime I run it, i get an null pointer exception when I run the appCaused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.w1481217.organiser.wordXMLList.populateList(wordXMLList.java:43)
This is my code:
public class wordXMLList extends Activity {
Intent i;
ListView lv;
ArrayAdapter<String> adapter;
ArrayList<String> xmlInString;
List<String> xml;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wordlisting);
i = getIntent();
xmlInString = i.getStringArrayListExtra("xml");
lv = (ListView) findViewById(R.id.listView2);
populateList(xmlInString);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, xml);
lv.setAdapter(adapter);
}
private void populateList(ArrayList<String> xmlToConvert) {
for(int i =0; i < xmlToConvert.size(); i++){
xml.add(xmlToConvert.get(i));
Log.d("threading", "final output in xmlToConvert: " + xmlToConvert.get(i));
}
}
}
Any help will be appreciated.