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

4
  • check this link: stackoverflow.com/questions/4780835/… Commented Aug 5, 2011 at 11:02
  • Check it out this link may be helpful... stackoverflow.com/questions/6355787/… Commented Aug 5, 2011 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));. Commented Aug 5, 2011 at 12:42
  • OH! You can't pass an ArrayList<String> into myIntent.putExtra(). You need to convert your ArrayList into a String[] first. Try putExtra((String[])arr.toArray()); Commented Aug 5, 2011 at 12:45

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

Use Bundle like this:

Bundle bundle = new Bundle();
bundle.putStringArray(key, news);
myIntent.putExtra("pass",bundle);
startActivity(myIntent);

3 Comments

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.
are you still getting the java.lang.NullPointerException error..Explain where are you getting it.......
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...

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.