Note:This question is not about how to fix a NullPointerException. This question is about why a piece of code that has been updated still generates a stacktrace in the Android Studio application for an older version.
I have a class that checks internet connectivity. This class, however, used to have an invalid piece of code, like this:
public class InternetConnectivityChecker {
private Context context;
public InternetConnectivityChecker(Context context) {
context = context;
}
I fixed this piece of code by changing it to:
public class InternetConnectivityChecker {
private Context context;
public InternetConnectivityChecker(Context context) {
this.context = context;
}
I kept getting a stacktrace, pasted below. To fix that, I decided to remove the invokation of the class altogether.
This class used to be called from SamaWebViewClient
, on line 22. However, now it is not being called anymore:
public class SamaWebViewClient extends WebViewClient {
private com.sama7.sama.listeners.OnReceivedErrorListener onReceivedErrorListener;
public SamaWebViewClient(com.sama7.sama.listeners.OnReceivedErrorListener listener) {
this.onReceivedErrorListener = listener;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
onReceivedErrorListener.onReceivedError();
}
}
However, now when I run my code in debug mode, even though I get 0 errors and 0 warnings in the Gradle Build
window, this is what I see in logcat:
03-02 08:49:29.240 24373-24373/com.sama7.sama E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sama7.sama, PID: 24373
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at com.sama7.sama.InternetConnectivityChecker.isConnected(InternetConnectivityChecker.java:15)
at com.sama7.sama.SamaWebViewClient.onReceivedError(SamaWebViewClient.java:22)
at com.android.webview.chromium.WebViewContentsClientAdapter.onReceivedError(WebViewContentsClientAdapter.java:582)
at org.chromium.android_webview.AwContentsClient.onReceivedError(AwContentsClient.java:355)
at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:160)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
03-02 08:49:38.015 24373-24373/com.sama7.sama I/Process: Sending signal. PID: 24373 SIG: 9
Why am I not getting the latest application built and deployed to my Samsung Galaxy S5 device?
For anyone discovering this in the future, here is what worked for me.
Turning Instant Run Off
If you need to turn off Instant Run, go to Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run… .
This is an excerpt from the Android tools page on instant run.