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.