我现在每次都会收到这个错误
inside public void onResponse(呼叫,响应响应){:
inside public void onResponse(呼叫,响应响应){:
mContext.runOnUiThread(new Runnable() { @Override public void run() { LocalDBUtility local=LocalDBUtility.getInstance(); //code }
在我的单例类LocalDBUtility中
static LocalDBUtility localDBUtility; private LocalDBUtility(){ } public static LocalDBUtility getInstance(){ if(localDBUtility==null){ synchronized (localDBUtility) { localDBUtility = new LocalDBUtility(); } return localDBUtility; }else { return localDBUtility; } }
我想知道是什么导致了NPE?
堆栈跟踪:
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
解决方法
if(localDBUtility==null){ synchronized (localDBUtility) {
如果localDBUtility为null,则无法对其进行同步调用.您只能在实例化对象上进行同步.