0

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.

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59

1 Answers1

-1
Try this:
    private void populateList(ArrayList<String> xmlToConvert) {
    xml = new ArrayList<>();
                for(int i =0; i < xmlToConvert.size(); i++){
                    xml.add(xmlToConvert.get(i));
                    Log.d("threading", "final output in xmlToConvert: " + xmlToConvert.get(i));
                }
    }
Khizar Hayat
  • 3,427
  • 3
  • 18
  • 22
  • Thanks for that. Just realised I forgot to insanitate the xml variable – Tarikh Chouhan Mar 18 '16 at 11:41
  • Welcome plz accept answer if helped – Khizar Hayat Mar 18 '16 at 11:41
  • But how are you creating an object of List type since its abstract? I am writiting the following and it works but I want to know why `List xml = new List<>();` doesnt work but `List();` doesn't work for me but It works for you – Tarikh Chouhan Mar 18 '16 at 11:43
  • @KhizarHayat this answer is not really helpful, since you don't even explain what did you change to make it work. Also, did you try the code? You cannot create a `new List<>()` in Java since `List` is an interface. – Tommaso Bertoni Mar 18 '16 at 12:22
  • Sorry i updated it to ArrayList. it is working – Khizar Hayat Mar 18 '16 at 12:25
  • @KhizarHayat Yes, but the problem that @TarikhChouhan has is that the `xml` variable is not yet instantiated, so it throws an exception. You have copy-pasted the method written in the question and added the creation of *one of many* List's implementation (ArrayList, LinkedList, Stack, etc). Also this question has been marked as duplicate, so there's no point in editing your "answer" now – Tommaso Bertoni Mar 18 '16 at 12:30
  • The instantiate problem is the only problem i saw in code so i added it to solve the issue. – Khizar Hayat Mar 18 '16 at 12:35