1

I'm trying to check if a service is running or not. If it's running I do want to send the sms. Unfortunately the app crashes when the service is not running and the sms does not send when the service is running.

Logcat:

E/SmsReciever﹕ Exeption smsReceiverjava.lang.NullPointerException

stop method in CallHelper class:

 public boolean stop() {
    tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
   // mainActivity.toggleUI();

    return false;
}

 public class IncomingSms extends BroadcastReceiver {

    final SmsManager sms = SmsManager.getDefault();
    private CallHelper callHelper;


    @Override
    public void onReceive(Context context, Intent intent) {

        final Bundle bundle = intent.getExtras();

                if(callHelper.stop()) {

                    try {


                        if (bundle != null) {

                            final Object[] pdusObj = (Object[]) bundle.get("pdus");

                            for (int i = 0; i < pdusObj.length; i++) {

                                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                                String senderNum = phoneNumber;
                                String message = currentMessage.getDisplayMessageBody();


                                String sms = "TEST";
                                try {
                                    SmsManager smsManager = SmsManager.getDefault();
                                    smsManager.sendTextMessage(senderNum, null, sms, null, null);

                                    Toast.makeText(context, "Sms sent Succesfully", Toast.LENGTH_LONG).show();

                                } catch (Exception e) {
                                    e.printStackTrace();
                                    Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
                                }


                                Log.i("SmsReciver", "senderNum: " + senderNum + "; message: " + message);

                                // Show Alert
                                int duration = Toast.LENGTH_LONG;
                                Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
                                toast.show();

                            } // End For loop
                        } // bundle is null

                    } catch (Exception e) {
                        Log.e("SmsReciever", "Exeption smsReceiver" + e);
                    }
                }
    }
}

UPDATE after initialising callhelper:

CallHelper callHelper = new CallHelper();

it's now giving me a nullpointerException on my stop method. I dont know why. it's a boolean method which returns true.

CallHelper Class:

public class CallHelper {

public Boolean calling = false;
private String phoneNumber ="0";
private String lastNumber;
private int count=0;


public CallHelper() {
}

// Listener to detect incoming calls.
public class  CallStateListener extends PhoneStateListener{
    @Override


    public  void onCallStateChanged (int state, String incomingNumber) {
        switch (state){...}

private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
private MainActivity mainActivity;

public CallHelper(Context ctx){
    this.ctx = ctx;

    callStateListener = new CallStateListener();
}

public void SendMessage(){...}

// Start Call Detection.
public void start() {
    tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

}

// Stop Call Dectection

public boolean stop() {
    tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
   // mainActivity.toggleUI();

    return true;
}

}

UPDATED IncomingSMS class

public class IncomingSms extends BroadcastReceiver {

final SmsManager sms = SmsManager.getDefault();
private CallDetectService callDetectService;

public IncomingSms(){}


@Override
public void onReceive(Context context, Intent intent) {

    final Bundle bundle = intent.getExtras();

    callDetectService = new CallDetectService();
    Intent intent1 = new Intent(context,CallDetectService.class);

    if (!callDetectService.stopService(intent1)) {

        try {


            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();


                    String sms = "TEST";
                    try {
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(senderNum, null, sms, null, null);

                        Toast.makeText(context, "Sms sent Succesfully", Toast.LENGTH_LONG).show();

                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
                    }


                    Log.i("SmsReciver", "senderNum: " + senderNum + "; message: " + message);

                    // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
                    toast.show();

                } // End For loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReciever", "Exeption smsReceiver" + e);
        }
    }

}

}

Andrew Irwin
  • 691
  • 12
  • 40
  • Change lot to Log.e("SmsReciever", "Exeption smsReceiver.", e); and attach full log. – Tronum Jul 10 '15 at 10:54
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) ... obviously something in the second line of the onRecive is null – Selvin Jul 10 '15 at 10:54

1 Answers1

0

Your exception may be occures after,

 final Object[] pdusObj = (Object[]) bundle.get("pdus");

hear bundle should not contain any Object with key "pdus" so pdusObj will remains null and in for loop you trying to get length by using "pdusObj.length" and you are getting java.lang.NullPointerException.