0

I have this error appearing in my logcat, with no additional information.

Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

While debugging I found out that this part generates the error:

PendingIntent reply = intent.getParcelableExtra(PENDING_RESULT_EXTRA);
if (reply != null) {
if (requestType == 1) {
    try {
        if (! googInfo.equals("") || googReach) {
            googReach = true;
        }

        if (! tradInfo.equals("") || tradReach) {
            tradReach = true;
        }

        if (tradReach && googReach) {// && ! tooSlow) {
            reply.send(RESULT_BOTH_AVAILABLE);
        } else if (tradReach) {// && ! tooSlow) {
            reply.send(RESULT_TRAD_AVAILABLE);
        } else if (googReach) {// && ! tooSlow) {
            reply.send(RESULT_GOOG_AVAILABLE);
        } else {
            reply.send(RESULT_UNAVAILABLE);
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
} else {
    try {
        if (! googInfo.equals("") || ! tradInfo.equals("") || googReach || tradReach) {
            reply.send(RESULT_AVAILABLE);
        } else {
            reply.send(RESULT_UNAVAILABLE);
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}
}

To be more precise the part where I do the following:

reply.send(RESULT_AVAILABLE);

This is an IntentService which is started by an Activity. While debugging I could see the reply object is correctly filled in and RESULT_AVAILABLE is a static final int which also has its correct value.

This IntentService is called with the following code:

PendingIntent pendingResult = createPendingResult(INTERNET_AVAILABILITY_CODE, new Intent(), 0);
Intent intent = new Intent(getApplicationContext(), PingIntentService.class);
intent.putExtra(PingIntentService.PENDING_RESULT_EXTRA, pendingResult);
startService(intent);

The weird thing is, I didn't change anything in these parts of the code and it always worked correctly. The other strange thing is my try / catch block doesn't catches the exception. I've put breakpoints and more information in that catch so that I could try to get more information about the error but that part is not triggered.

I've already googled the error and many people seem to have this problem in other situations. I understand my context is null, but while debugging I can see it is not null... Even with all the links / answers that I've found I still can't find the source of my problem.

M3-n50
  • 803
  • 1
  • 13
  • 23

2 Answers2

1

just as a side node.

make your code null safe with this kind of condition:

"".equals(tradInfo)

with tradInfo.equals("") you will get a NullPointerException if tradInfo is null(and maybe that is a valid case?). If you toggle it that can not occur.

nano_nano
  • 12,351
  • 8
  • 55
  • 83
0

While continuing the search for a solution I've stumbled upon the answer thanks to CommonsWare. The error itself wasn't in this part:

reply.send(RESULT_AVAILABLE);

I was searching why I didn't see anything in the Java stack trace. I thought the error should've been caught in the try / catch block in the code above, but the error occurred when the Activity got results back from the IntentService.

There was indeed a piece of the code where my Context object was still Null. I changed it to getApplicationContext() and it was fixed. This error had never occurred before because it only occurs on the very first time the application is started in a device. It's still a piece of code that was added in the very beginning in which I stored my application context and passed it around. When starting for the first time this object wasn't filled in yet and this error occurs.

M3-n50
  • 803
  • 1
  • 13
  • 23