变量值可用但始终显示空指针异常。如何解决它

时间:2018-03-12 22:57:33

标签: android string nullpointerexception

if(  str_driversig != null && str_driversig.length()>0 &&  !str_driversig.contentEquals(null))
    {
}else
    {
        Toast.makeText(context,"Please give Driver Signature",Toast.LENGTH_SHORT).show();
    }

这里str_driversig变量vaue像..... / .... / signature / 81046.png

始终显示以下错误:

UncaughtException: java.lang.NullPointerException: Null reference used for synchronization (monitor-enter)
   at java.lang.String.contentEquals(String.java:958)
   at com.main.exam.ui.driverchecklist.AddDriverChecklist.save_driver_checklist(AddDriverChecklist.java:1318)
   at com.main.exam.ui.driverchecklist.AddDriverChecklist.onClick(AddDriverChecklist.java:367)
   at android.view.View.performClick(View.java:6261)
   at android.widget.TextView.performClick(TextView.java:11185)
   at android.view.View$PerformClick.run(View.java:23752)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6776)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

1 个答案:

答案 0 :(得分:0)

您正在尝试检查数据字符串是否等于null,而不是!str_driversig.contentEquals("null")而不是!str_driversig.contentEquals(null)。在您的情况下null导致NPE。