0

i'm continuously running into problems trying to pass an ArrayList from one Activity to another. My code is failing with a Null Pointer Exception when i try to iterate through the ArrayList in my XMLParser Class. I've put print statements into the Activity that generates the ArrayList and it looks fine. Can anyone see what i'm doing wrong or why i get a Null pointer Exception when retrieving the ArrayList?

public void onClick(View v) {
    if (selItemArray[0] == null) {
        Toast.makeText(getApplicationContext()," Please make a Selection ", Toast.LENGTH_SHORT).show();
    } else {
        Intent intent = new Intent(Recipes2.this, XMLParser.class);
        Log.v("Recipes2", "selItemArray[0] before call to XML Parser : " + selItemArray[0]);
      //Log.v("Recipes2", "selItemArray[1] before call to XML Parser : " + selItemArray[1]);    
        intent.putExtra("items_to_parse",  selItemArray);
        startActivityForResult(intent, 0);              
    }
}




public class XMLParser extends ListActivity {
            /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Bundle b = getIntent().getExtras();
        //itemsToParse = b.getStringArrayList("items_to_parse");
        ArrayList<String> itemsToParse = new ArrayList<String>();
        itemsToParse = getIntent().getExtras().getStringArrayList("items_to_parse");        Iterator<String> iterator = itemsToParse.iterator();            while(iterator.hasNext())
                Log.v("XMLParser", iterator.next().toString());

2 Answers 2

1

It looks like you're putting a String array, not a ArrayList<String>.

Sign up to request clarification or add additional context in comments.

Comments

0

You used a string array on the "sender" side and are trying to get it back as an ArrayList on the receiver side. That won't work. Use a String array on both sides and -- if necessary -- pull it into an array list.

The procedure for passing the data is here:

Passing String array between two class in android application

To convert it to a List - simply do:

List<String> = Arrays.asList(stringArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.