0

I am a student and new to programming. It might be a simple error, can anybody help me fix it.I have to pass an array list from one activity to another. In this activity i have 5 radio buttons RB1, RB2.... and i want to pass the content of news[] it to another activity called display.

public void onClick(View v) {
        String[] news;
        news = new String[5];

        news[0] = "bbc";
        news[1] = "guardian";
        news[2] = "yahoo";
        news[3] = "sky";
        news[4] = "fox news";

        final ArrayList<String> arr = new ArrayList<String>();
        if (RB1.isChecked() == true)
            arr.add(news[0]);
        if (RB2.isChecked() == true)
             arr.add(news[1]);
        if (RB3.isChecked() == true)
            arr.add(news[2]);
        if (RB4.isChecked() == true)
            arr.add(news[3]);
        if (RB5.isChecked() == true)
            arr.add(news[4]);
if (v == Done)
        {
            Button done = (Button) findViewById(R.id.DONE);
            done.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view)
                {
                    Intent myIntent = new Intent(Read.this, display.class);
                    myIntent.putExtra("pass",arr);
                    startActivity(myIntent);
                }
            });
        }

the codes for next activity is as follows

                    Intent myintent = getIntent();
        String[] Array = myintent.getStringArrayExtra("pass");

        for (int i = 0; i < Array.length; i++)
            Log.v(LOG_TAG, "THE website Is :" +Array[i]);

I am geting a java.lang.NullPointerException in the above two line i.e

for (int i = 0; i < Array.length; i++)
                Log.v(LOG_TAG, "THE website Is :" +Array[i]);

can u pls tell me why ? thanks in advance

Divya
  • 3
  • 1
  • 3
  • check this link: http://stackoverflow.com/questions/4780835/pass-arraylist-from-one-activity-to-other/4781032#4781032 – Paresh Mayani Aug 05 '11 at 11:02
  • Check it out this link may be helpful... http://stackoverflow.com/questions/6355787/how-to-pass-arraylist-from-one-activity-to-another – Uttam Aug 05 '11 at 11:09
  • For your NullPointerException problem, the `Array` variable is probably null, which would happen if you didn't properly pass it in from the previous activity. Double check this with a log statement right after `String[] Array = ...`, something like `Log.v(LOG_TAG, "Array is null? " + (Array[i] == null));`. – Josh Aug 05 '11 at 12:42
  • OH! You can't pass an `ArrayList` into `myIntent.putExtra()`. You need to convert your `ArrayList` into a `String[]` first. Try `putExtra((String[])arr.toArray())`; – Josh Aug 05 '11 at 12:45

2 Answers2

2

First of all, some conventions: Start names of variables lowercase, so array instead of Array. This will save you from some confusion later.

Try it as follows, from this thread:

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

and

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
Community
  • 1
  • 1
nhaarman
  • 98,571
  • 55
  • 246
  • 278
1

Use Bundle like this:

Bundle bundle = new Bundle();
bundle.putStringArray(key, news);
myIntent.putExtra("pass",bundle);
startActivity(myIntent);
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • Thanks a lot for your help . Really appreciate it . Am afraid it dosent help me solve the java.lang.NullPointerException error. Can u pls help me how to solve this error. – Divya Aug 05 '11 at 11:18
  • are you still getting the java.lang.NullPointerException error..Explain where are you getting it....... – Vineet Shukla Aug 05 '11 at 11:20
  • please see the last part of the question.. the same point.. in the for loop.. can u please tell me what difference will it make if i pass arraylist to the 2nd activity without using bundle , using just intent as i did initially... – Divya Aug 05 '11 at 12:02