2

So I am confused and I don't know how to fix the error crashlytics and google playstore keep saying my users are having. Here is my code:

if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
            Notification notification = (Notification) event.getParcelableData();

String text = "";

if (notification.extras.getString(Notification.EXTRA_TEXT) != null){
                    text  = notification.extras.getString(Notification.EXTRA_TEXT);
                }

if (text != null) {
    if (text.equals("You have been idle, would you like to stay online?")
      startApp();
{

But crashlytics and google playstore say that I am referencing null Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'android.os.Bundle android.app.Notification.extras' on a null object reference, how is this possible when I am checking for null first? Here is the full error code:

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'android.os.Bundle android.app.Notification.extras' on a null object reference
   at com.gonow.services.AutoService.onAccessibilityEvent(Unknown Source)
   at android.accessibilityservice.AccessibilityService$2.onAccessibilityEvent(AccessibilityService.java:1472)
   at android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage(AccessibilityService.java:1608)
   at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6823)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)

I have already looked at this: What is a NullPointerException, and how do I fix it?

user2101081
  • 396
  • 3
  • 19
  • 1
    "how is this possible when I am checking for null first?" - You're checking that the `getString(Notification.EXTRA_TEXT)` return is not null. That Exception is telling you that `notification` is null. – Mike M. Jan 21 '18 at 02:24
  • How can it be null, when there is a notification drop down though? Like the notification still shows up. From your comment I understand I need to do if (notification != null), but wouldn't this just dismiss every notification? – user2101081 Jan 21 '18 at 02:26
  • "How can it be null, when there is a notification drop down though?" – I haven't worked with `AccessibilityService` in a while, but I would think that `TYPE_NOTIFICATION_STATE_CHANGED` will fire when any `Notification` is cleared/dismissed, in which case, I would expect that the data `Notification` would be null on that event. "...but wouldn't this just dismiss every notification?" – You can still do your `text.equals()` check; just do the `notification != null` check first. – Mike M. Jan 21 '18 at 02:38
  • Are you getting this mainly on android 8? – casolorz Jan 25 '18 at 16:46

0 Answers0