Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get NullPointerException when sending an SMS on an HTC Desire, or what is SubmitPdu?

Tags:

android

sms

So I get this stack trace:

java.lang.NullPointerException    
   at android.telephony.SmsMessage$SubmitPdu.<init>(SmsMessage.java:132)
   at android.telephony.SmsMessage.getSubmitPdu(SmsMessage.java:551)
   at android.telephony.SmsManager.sendTextMessage(SmsManager.java:228)
   at android.telephony.SmsManager.sendTextMessage(SmsManager.java:107)
   at com.emergency.button.SMSSender.safeSendSMS(SMSSender.java:91)
   at com.emergency.button.EmergencyActivity$EmergencyThread
       .sendSMS(EmergencyActivity.java:294)
   at com.emergency.button.EmergencyActivity$EmergencyThread
       .sendMessages(EmergencyActivity.java:386)
   at com.emergency.button.EmergencyActivity$EmergencyThread
       .run(EmergencyActivity.java:266)

And this is how I call sendTextMessage, I don't verify message length or phone number validity:

sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

This error seems to only occur on HTC Desire or HTC Wildfire with android version 2.2 or 2.21 so I dug up the android platform source (which is actually not as easy to do as I expected) and found:
SmsMessage.java once upon a time though this might not be relevant as I see some of the line numbers don't line up.
The emergency button project is open source so you can check the code out for reference. I should probably note that I can't test this myself, I don't have either phone model.

like image 432
ubershmekel Avatar asked Jan 02 '11 22:01

ubershmekel


2 Answers

You might be hitting this issue with a message that is too long. I don't know why it would only be happening for certain phones, though.

http://code.google.com/p/android/issues/detail?id=3718

like image 137
johnwayner Avatar answered Oct 22 '22 03:10

johnwayner


Replace

sms.sendDataMessage(destinationAddress, scAddress, destinationPort, data, sentIntent, deliveryIntent)

by

sms.sendMultipartTextMessage(phoneNumber, null, message, null, null);

Your message must be an ArrayList for that, use this method before calling sendSMS

SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(locationMessage);
sendSMS(telNumber.getText().toString(),parts );
like image 29
Anass Avatar answered Oct 22 '22 03:10

Anass