4

I have a bug from the SMSManager when I try to send a message with the method sendTextMesasge. It returns me a java.lang.NullPointerException: Attempt to get length of null array.

There is my code :

        // Constructs the message
        TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);     
        String myPhoneNumber = mTelephonyMgr.getLine1Number();
        String destNumber = taskManager.getHelpCenterNumber();
        String message = getResources().getString(R.string.call_me_message) + " " + myPhoneNumber+".";

        // Sends the message
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(destNumber, null, message, null, null);

The LogCat receives this message :

08-03 09:59:10.548: E/AndroidRuntime(19825): FATAL EXCEPTION: main
08-03 09:59:10.548: E/AndroidRuntime(19825): Process: com.solarsquareretailer.view, PID: 19825
08-03 09:59:10.548: E/AndroidRuntime(19825): java.lang.NullPointerException: Attempt to get length of null array
08-03 09:59:10.548: E/AndroidRuntime(19825):    at android.os.Parcel.readException(Parcel.java:1546)
08-03 09:59:10.548: E/AndroidRuntime(19825):    at android.os.Parcel.readException(Parcel.java:1493)
08-03 09:59:10.548: E/AndroidRuntime(19825):    at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:1428)
08-03 09:59:10.548: E/AndroidRuntime(19825):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:305)
08-03 09:59:10.548: E/AndroidRuntime(19825):    at com.solarsquareretailer.view.MainActivity.onOptionsItemSelected(MainActivity.java:245)
Sigvent
  • 297
  • 4
  • 17

3 Answers3

5

Found my destNumber was null and not correct (Not a phone number). It's why I have a java.lang.NullPointerException.

Sigvent
  • 297
  • 4
  • 17
1

I've found this several of times in my app in the wild (but never on my tests), and tried to find a solution for months. So far I could not find a test case that reproduces it.

Using the Sms.sendMultipartTextMessage method:

SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(msg);
sms.sendMultipartTextMessage(number, null, parts, null, null);

Does not help either - as internally it ends up calling sendTextMessage if the message has only one part - but anyway, it might be a good idea if you switch to it just in case you have longer messages.

I ended up wrapping the code in a try-catch and reporting to a server whenever it happens. And from what I've seen, seemingly 'normal' messages seem to trigger it.

There is a similar case here, but in that case the message has a PendingIntent for sent. You might want to try that.

Community
  • 1
  • 1
Sebastian
  • 1,076
  • 9
  • 24
1

First , you need permission in AndroidManifest.xml for read Phone State

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

Then try to get Mobile Number of Device in Try Catch Block .

It may happen that getLine1Number() return null value . So you need to handle exception

try 
{
    myPhoneNumber = mTelephonyMgr.getLine1Number();
}
catch(NullPointerException ex)
{
}
Kiran Choudhary
  • 1,125
  • 7
  • 15