0

I have a AsyncTask in a Service. I send an ArrayList as broadcast from the AsyncTask.

When I get the ArrayList in onReceive() I get a NullpointerException.

This is how I send the ArrayList.

 transits_list = new ArrayList<Transit>();
 transits_list.add(trs);

Intent arrayListIntent = new Intent("arrayList");
Bundle extra = new Bundle();
extra.putSerializable("transArray", transits_list);
intent.putExtra("extra", extra);
sendBroadcast(arrayListIntent);

The Transit class implements Serializable.

Receiving the ArrayList

@Override
    public void onReceive(Context context, Intent intent) {
        ArrayList<Transit> myList;

        Bundle extra = getIntent().getBundleExtra("extra");
        ArrayList<Transit> transArrayListFromBroadCast =     (ArrayList<Transit>) extra.getSerializable("transArray");
        System.out.print("transArrayListFromBroadCast "+transArrayListFromBroadCast);
    }

I get NullpointerException in this line:

ArrayList<Transit> transArrayListFromBroadCast =     (ArrayList<Transit>) extra.getSerializable("transArray");

The exception from log:

FATAL EXCEPTION: main java.lang.RuntimeException: Error receiving broadcast Intent { act=arrayList flg=0x10 } in com.prematix.tollsystem.avcc.AvccActivity$ArrayListReceiver@42003268 at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:798) at android.os.Handler.handleCallback(Handler.java:800) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5391) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.prematix.tollsystem.avcc.AvccActivity$ArrayListReceiver.onReceive(AvccActivity.java:271) at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:788) at android.os.Handler.handleCallback(Handler.java:800)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loop(Looper.java:194)  at android.app.ActivityThread.main(ActivityThread.java:5391)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:525)

1 Answer 1

0

Your method of getting the Intent is incorrect. I believe your BroadcastReceiver is in an activity class since, you are calling getIntent(). However, getIntent() will get you the Intent supplied to the activity and not the receiver. The Intent for receiver is provided to the method onReceive() itself. Make the following changes to your code:

Adding extra:

Intent arrayListIntent = new Intent("arrayList");
Bundle extra = new Bundle();
extra.putSerializable("transArray", transits_list);
intent.putExtra("extra", extra);
sendBroadcast(arrayListIntent);

Getting Extra:

@Override 
public void onReceive(Context context, Intent intent) {
    ArrayList<Transit> myList;

    Bundle extra = intent.getBundleExtra("extra");
    ArrayList<Transit> transArrayListFromBroadCast = (ArrayList<Transit>) extra.getSerializable("transArray");
    // System.out.print("transArrayListFromBroadCast "+transArrayListFromBroadCast);
} 
1
  • Thanks. I will try and let you know
    – Varun
    Oct 16, 2016 at 3:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.