java – Android:NullPointerException尽管@NonNull
作者:互联网
我正在使用注释来确保参数不为null,假设这会导致编译器检查.
public @Nullable ApplicationAccount accountForKey(@NonNull String key) {
return accounts.get(key);
}
但是,通过运行此代码,我在此行上得到了NullPointerException
java.util.concurrent.ConcurrentHashMap.get (ConcurrentHashMap.java:883)
那么注释有什么意义呢?
如果我像这样写额外的支票,那就更加模糊了
return key!=null?accounts.get(key):null;
Android Studio警告我,检查没用!
更新:完整调用堆栈:
Caused by java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:883)
at co.getcontrol.services.MerchantCenter.accountForKey(MerchantCenter.java:72)
at co.getcontrol.model.customers.CustomersAggregator.loadCustomerDetails(CustomersAggregator.java:91)
at co.getcontrol.model.customers.CustomerDetailsPresenter.callData(CustomerDetailsPresenter.java:39)
at co.getcontrol.reskin.ui.customers.CustomerDetailsViewFragment.onCreateView(CustomerDetailsViewFragment.java:152)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:602)
at co.getcontrol.ui.ControlActivity.onStart(ControlActivity.java:13)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
at android.app.Activity.performStart(Activity.java:5353)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2352)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5345)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(NativeStart.java)
解决方法:
注释创建合同. @NonNull说这个方法不接受null并且传递它可能会使程序崩溃(这正是发生的事情). Android Studio会警告此方法的任何用法,它可以推断出null可能会被传递.但它不会阻止传递null.
非常相似的契约是[]的参数,对于不能超出其边界(例如小于0)的数组,但仍然不会阻止开发人员传递超出边界的值.
对null的检查被标记为多余的,因为通过契约,永远不应该为null.如果添加检查,该方法现在将知道如何处理null,因此不应标记为@NonNull.
标签:java,android,nullpointerexception,android-annotations,non-nullable 来源: https://codeday.me/bug/20190527/1166055.html