与 java 中的空值进行比较时出现 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17850003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 17:47:54  来源:igfitidea点击:

NullPointerException when comparing with null values in java

javaexceptionif-statementnullnullpointerexception

提问by praveenraj4ever

if(status.equals(null))
{
  status="Pass";
}

from above code it throws NullPointerException, please give solution for comparing value with null.

从上面的代码抛出NullPointerException,请给出与null.

回答by Maroun

Looks like statusitself is null. So when you do:

看起来status自己是null。所以当你这样做时:

status.equals("null")

You're actually doing:

你实际上在做:

null.equals("null")

Which causes NPE. You should do:

这会导致 NPE。你应该做:

if(status == null) //Now you're checking if the reference is null
                   //So you'll never dereference a null pointer

You might find this linkuseful.

您可能会发现此链接很有用。



Related topics:

相关话题

What is null in Java?
Java null check why use == instead of .equals()

Java 中的 null 是什么?
Java null 检查为什么使用 == 而不是 .equals()

回答by Josnidhin

In Java "null"is a string. You want to compare the reference to null. Do the following:

在 Java 中"null"是一个字符串。您想将引用与 null 进行比较。请执行下列操作:

if(status == null){
  // code here
}

回答by Freak

Keep in mind that String is class in Java.So whenever you call a method with unitialized object (which is a null object) then it will throw NullPointerException.In your case, there is possibility that your String statusis not initialized So you need to make it sure.
BTW you should check nullwithout using equals()method because it doesn't make sense that you are checking a null object's method for its null value.
You must do like this

请记住,String 是 Java 中的类。因此,每当您使用未初始化的对象(这是一个空对象)调用方法时,它都会抛出NullPointerException。在您的情况下,您可能String status未初始化所以您需要确保.
顺便说一句,您应该在null不使用equals()方法的情况下进行检查,因为检查空对象的方法的空值是没有意义的。
你必须这样做

if(status == null)
//Do something


only use equals method when you want to compare a String and at the stage where you are quiet sure that your String is initialized.Let say String status = "";is a intialized String and now it is not null.
Just for the info
while using equals(), try to use it like "anyValue".equals(status)instead of status.equals("anyValue")because by using like "anyValue".equals(status), it will be more safe in case of null string and you wont get NullPointerException


仅当您想比较字符串时才使用 equals 方法,并且在您确定您的字符串已初始化的阶段。假设String status = "";是一个初始化的字符串,现在它不为空。
只为信息
,而使用equals()时,尽量使用像"anyValue".equals(status),而不是status.equals("anyValue")使用像,因为"anyValue".equals(status),这将是空字符串的情况下更加安全,你不会得到NullPointerException

回答by Aniket Thakur

if(status.equals("null")) //Just checking if string content/value is same
{
status="Pass";
}

By this you are just checking if value(content) of status variable is "null" String or not. IF you want to do a null check you need to do the following

通过这种方式,您只是检查状态变量的值(内容)是否为“空”字符串。如果要进行空检查,则需要执行以下操作

if(null == status)
{
status="Pass";
}

回答by Mohan Raj

You are not suppose to make it null as "null"! in your case compiler consider it as string.

您不应该将其设为 null 为“null”!在您的情况下,编译器将其视为字符串。

if(stringVariable!=null)
{
    //ur Code
}

(OR)

(或者)

if(stringVariable.equals(null))
{
    //Ur code
}

In case if ur working with string array you can do it as following

如果您使用字符串数组,您可以按以下方式进行

if(stringArray.isEmpty())//checks length of String array 
{
//ur code
}

回答by Shuhail Kadavath

You cannot use .equals with a null . Use :

您不能将 .equals 与 null 一起使用。利用 :

if(status == null) {
//Do something 
}

This is exactly the reason why the HashMap cannot store more than one null values . Because it always compares the key with other valuues and if a null is inputted the second time , it throws NPE .

这正是 HashMap 不能存储多个空值的原因。因为它总是将键与其他值进行比较,如果第二次输入空值,它会抛出 NPE。

回答by Ruchira Gayan Ranaweera

You are getting this error since statusis null.

您收到此错误,因为statusnull