-1

Working on an SMS app, which crashes upon reciept of a new message. The issue seems to lie in the Broadcast Receiver for incoming SMS but I can't figure out why.

The Broadcast Receiver:

public class SMSBroadcastReceiver extends BroadcastReceiver {

     public static final String SMS_BUNDLE = "pdu";

     public void onReceive(Context context, Intent intent) {
         Bundle intentExtras = intent.getExtras();

         if (intentExtras != null) {
             Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
             String smsMessageStr = "";
             for (int i = 0; i < sms.length; ++i) {     //Line 24
                 String format = intentExtras.getString("format");
                 SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);

                 String smsBody = smsMessage.getMessageBody().toString();
                 String address = smsMessage.getOriginatingAddress();

                 smsMessageStr += "SMS From: " + address + "\n";
                 smsMessageStr += smsBody + "\n";
             }

             Toast.makeText(context, "Message Received!", Toast.LENGTH_SHORT).show();

             if (MainActivity.active) {
                 MainActivity inst = MainActivity.instance();
                 inst.updateInbox(smsMessageStr);
             } else {
                 Intent i = new Intent(context, MainActivity.class);
                 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 context.startActivity(i);

             }
         }
     }
 }

I labeled line 24 where the exception gets called.

The Error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.haltaar.texting, PID: 15909
    java.lang.RuntimeException: Unable to start receiver com.example.haltaar.texting.SMSBroadcastReceiver: java.lang.NullPointerException: Attempt to get length of null array
      at android.app.ActivityThread.handleReceiver(ActivityThread.java:3194)
      at android.app.ActivityThread.-wrap17(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
      at android.os.Handler.dispatchMessage(Handler.java:106)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6494)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
    Caused by: java.lang.NullPointerException: Attempt to get length of null array
      at com.example.haltaar.texting.SMSBroadcastReceiver.onReceive(SMSBroadcastReceiver.java:24)
      at android.app.ActivityThread.handleReceiver(ActivityThread.java:3187)
      at android.app.ActivityThread.-wrap17(Unknown Source:0) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672) 
      at android.os.Handler.dispatchMessage(Handler.java:106) 
      at android.os.Looper.loop(Looper.java:164) 
      at android.app.ActivityThread.main(ActivityThread.java:6494) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Can anyone help shed some light on this? AFAIK all permissions are being called correctly, and the check to see if intentExtras is null should stop the sms object from being null when checked? Or am I missing something?

Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
SethJames
  • 1
  • 1
  • For incoming SMS, the correct key for the `Intent` extra is `"pdus"`, not `"pdu"`. Also, for versions starting with KitKat, you should instead use the `Telephony.Sms.Intents.getMessagesFromIntent()` method, which takes care of retrieving the PDUs, and creating the `SmsMessage`s for you. – Mike M. Jan 30 '18 at 22:48

1 Answers1

0

check intent.hasExtra(SMS_BUNDLE) before calling intent.get(SMS_BUNDLE) to prevent null pointer exception

Jarvis
  • 1,714
  • 2
  • 20
  • 35