BroadcastReceiver - Object reference not set to an instance of an object.

I receive my message and I can print it in my console the message that I received but when I pass my message into the interface, there's an error which is the
"System.NullReferenceException: Object reference not set to an instance of an object.
at (wrapper dynamic-method) System.Object:03a009aa-80c5-4971-a632-c964004d4429 (intptr,intptr,intptr,intptr)
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
at (wrapper dynamic-method) System.Object:03a009aa-80c5-4971-a632-c964004d4429 (intptr,intptr,intptr,intptr)"

I don't get know, where is the null value. Thanks for the help.

Class for BroadcastReceiver

 class SmsReceiver : BroadcastReceiver
    {
    public static readonly string intentAction = "android.provider.Telephony.SMS_RECEIVED";
    private OnReceiveSMSListener _onReceiveSMSListener;

     public override void OnReceive(Context context, Intent intent)
     {
        if (intent.Action != intentAction) return;
        var bundle = intent.Extras;
        if (bundle == null) return;

        var pdus = bundle.Get("pdus");
        var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
        var messages = new SmsMessage[castedPdus.Length];

        var sb = new StringBuilder();
        string sender = null;
        for (var i = 0; i < messages.Length; i++)
        {
                var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
                JNIEnv.CopyArray(castedPdus[i].Handle, bytes);

                 string format = bundle.GetString("format");
                 messages[i] = SmsMessage.CreateFromPdu(bytes, format);

                 if (sender == null)
                 sender = messages[i].OriginatingAddress;

                 sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", messages[i].OriginatingAddress, System.Environment.NewLine, messages[i].MessageBody));

                  Console.WriteLine("MESSAGE " + sb.ToString());
                   _onReceiveSMSListener.onReceived(sb.ToString()); **// Object reference not set to an instance of an object.**
                }
     }

    public interface OnReceiveSMSListener
    {
        void onReceived(string message);
    }

        public void setOnReceiveListener(OnReceiveSMSListener onReceiveSMSListener)
        {
            _onReceiveSMSListener = onReceiveSMSListener;
        }
}

MainActivity

public class MainActivity: BaseActivity, SmsReceiver.OnReceiveSMSListener
{

        private SmsReceiver smsBroadcastReceiver;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        smsBroadcastReceiver = new SmsReceiver();
            smsBroadcastReceiver.setOnReceiveListener(this);
    }

     public void onReceived(string message)
     {
            string strNum = new string(message.ToString().Where(c => char.IsDigit(c)).Take(4).ToArray());
            Console.WriteLine("Message" + strNum);
     }

}

Best Answer

  • kmmpalizakmmpaliza ✭✭
    Accepted Answer

    I solved it by adding
    if (_onReceiveSMSListener != null)
    {
    _onReceiveSMSListener.onReceived(sb.ToString());
    }
    inside the OnReceive(Context context, Intent intent)

Answers

  • kmmpalizakmmpaliza Member ✭✭
    Accepted Answer

    I solved it by adding
    if (_onReceiveSMSListener != null)
    {
    _onReceiveSMSListener.onReceived(sb.ToString());
    }
    inside the OnReceive(Context context, Intent intent)

  • yelinzhyelinzh Member, Xamarin Team Xamurai
    edited March 5

    Congrats, and thanks very much for sharing the solution.

Sign In or Register to comment.