0

Calling this method in my Receiver class that extend BroadcastReceiver to cancel incoming call.There is no error in code at compile time but when this method is called the exception is thrown that is.

Exception

java.lang exception android.content.context.getsystemservice(java.lang.string)' on a null object reference

Code

 private void disconnectPhoneItelephony(Context context)
{
    ITelephony telephonyService;
    TelephonyManager telephony = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    try
    {
        Class c = Class.forName(telephony.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        telephonyService = (ITelephony) m.invoke(telephony);
        telephonyService.endCall();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66

1 Answers1

1

Make sure that you properly forward the Context to your method. It seems that the context somehow gets lost during public abstract void onReceive (Context context, Intent intent)

To properly answer this, please show the entire class and make sure that disconnectPhoneItelephony() is called with the correct Context instance.

Martin C.
  • 12,140
  • 7
  • 40
  • 52