I get this error now everytime
inside public void onResponse(Call call, Response response){:
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
LocalDBUtility local=LocalDBUtility.getInstance();
//code
}
inside my singleton class LocalDBUtility
static LocalDBUtility localDBUtility;
private LocalDBUtility(){
}
public static LocalDBUtility getInstance(){
if(localDBUtility==null){
synchronized (localDBUtility) {
localDBUtility = new LocalDBUtility();
}
return localDBUtility;
}else {
return localDBUtility;
}
}
I want to know what caused NPE?
Stack Trace:
java.lang.NullPointerException: Null pointer exception during instruction 'monitor-enter v1'
at com.utilities.LocalDBUtility.getInstance(LocalDBUtility.java:34)
at com.MainActivity$4.run(MainActivity.java:386)
at android.app.Activity.runOnUiThread(Activity.java:5506)
at
retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5763)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755
NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.
What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.
NullPointerException is a runtime exception and it is thrown when the application try to use an object reference which has a null value. For example, using a method on a null reference.
if(localDBUtility==null){
synchronized (localDBUtility) {
If localDBUtility is null, you can't call synchronize on it. You can only synchronize on an instantiated object.
In Java every instance of a reference type has its own monitor associated with it. The synchronized block uses this monitor, locking it when a thread enters and unlocking it when this same thread leaves it. If you pass a null reference to the synchronized block, the operation that tries to get the associated monitor will be called on a null reference that results in a NullPointerException. Hence, you can not synchronize on null references.
You may already see that the code you are to running is trying to use a null reference ("localDBUtility==null") for the synchronized block which obviously results in an NPE.
Note: A monitor is a synchronization construct that is used to implement mutual exclusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With