String 说它不为 null,但随后抛出 NullPointerException
我的天啊。我有一个小项目要做,弦乐快要杀了我!
现在,我有一个 null
的字符串(从 servlet 调用 getParameter()
获取值)。
问题是,我试图查看它是否为空,并且即使它为空,在程序中也会告诉我它不是 null
,但稍后在程序中,当我使用变量,我收到一个异常,指出该变量为 null
。
System.out.println("In " + ID); // in console: In null
if ((ID == null) || (ID == "null") || ID.equals(null) || **ID.equals("null")**)
{
// after I put the 4th condition, the if is working right (WHAT IS THE PROBLEM?)
System.out.println("==null");
this.ID = "";
}
else
{
System.out.println("!=null");
this.ID = ID;
}
System.out.println("After " + ID);
我做错了什么?
只有第四个条件有效!剩下的呢(除了第二个,因为这个条件是我因为绝望而提出的)
我教 ID == null
或 ID.equals(null)
就可以了,但是没有。
编辑: 问题是,我从表单(通常是表单 1)获取 ID 的值。但在本例中,我使用的表单 2 没有任何 ID 输入,因此 ID 必须为 null
而不是 "null"
OMG. I have a little project to do and the Strings are killing me!
Now, I have a String which is null
(is taken the value from invoking getParameter()
from a servlet).
The problem is that, I'm trying to see if it's null, and, even if it's null, in the program is telling me that is not null
, but later in program, when I'm using the variable, I receive a exception saying the variable is null
.
System.out.println("In " + ID); // in console: In null
if ((ID == null) || (ID == "null") || ID.equals(null) || **ID.equals("null")**)
{
// after I put the 4th condition, the if is working right (WHAT IS THE PROBLEM?)
System.out.println("==null");
this.ID = "";
}
else
{
System.out.println("!=null");
this.ID = ID;
}
System.out.println("After " + ID);
What I'm doing wrong?
Only the forth condition is working! What about the rest(except second one, because that condition i put it because I was desperate)
I taught ID == null
or ID.equals(null)
will be ok, but no.
Edit:
The problem is that, I'm getting the value of the ID from a form(form 1 let's say- usually). But in this case, I'm using form 2 which doesn't have any ID inputs, so ID must be null
and not "null"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
显然,
ID
包含四个字母的字符串"null"
。所以它不是null
(“nothing”的值)。有关
null
常量的更多信息,请参阅 Java 术语表。基本上,如果变量不引用任何对象,则其值为null
。然而,字符串“null”是一个对象,即类的实例String,在本例中变量ID
引用此对象。(请注意,按照惯例,Java 变量以小写字母开头,而像 ID 这样的缩写词则完全小写,因此请写
id
而不是ID
。)< /子>Clearly,
ID
contains the four-letter string"null"
. So it's notnull
(the value for "nothing").See the Java glossary for more on the
null
constant. Basically a variable has the valuenull
if it does not reference any object. The string"null"
is an object however, namely an instance of the class String, and in this case the variableID
references this object.(Note that by convention Java variables start with a lower case letter, and acronyms like ID are written completely lower case, so write
id
instead ofID
.)这是您尝试过的四个测试。您只需要第一个和第四个。
ID == null
:字段“ID”是否为空?ID == "null"
:字段“ID”的引用是否与新分配的String
“null”相同?这通常应该返回 false。ID.equals(null)
:这应该始终返回 false - 从概念上讲,如果这是真的,您应该抛出NullPointerException
。ID.equals("null")
:String
'ID' 的值与String
“null” 的值相同?Here are the four tests you've tried. The first and the fourth are the only ones that you should need.
ID == null
: is the field 'ID' null?ID == "null"
: is the ref for the field 'ID' the same as the newly allocatedString
"null"? This should generally return false.ID.equals(null)
: this should always return false - conceptually were this ever true you should throw aNullPointerException
.ID.equals("null")
: is the value of theString
'ID' the same as the value of theString
"null"?由于您从 servlet 获取字符串,我可以说这是正常的。
在某些情况下,Java 会将空字符串转换为“null”字符串。
显然,您检索到的字符串不是空值,而是一个 4 字符字符串“null”,
为什么不尝试调试呢?或者只是看看它返回什么:
编辑:如果你在这里没有得到异常,这意味着该字符串不为空,并且输出“ID的长度:4”将意味着该字符串实际上是ID =“null”
EDIT2 :好吧,似乎有些人不明白这里发生了什么,他们说在 Java 中的某些情况下空字符串怎么会是“null”?他们觉得这很可笑。我更喜欢他们在 java 上尝试这个:
输出将是“hellonull”没有别的...
这里我们还有一个 servlet。存在空数据。 Servlet 将空数据作为“null”发送,该怎么办?空字符串?快点!!! ”
Since you get the string from a servlet i can say that this is normal.
Java converts a null string to a "null" string on some conditions.
Obviously the string you retrieve is not a null value, but it is a 4 char string "null"
Why don't you try debugging? Or just see what does this return:
Edit: If you don't get exception here, this means that the string is not null and also output "Length of ID: 4" will mean that the string is really ID = "null"
EDIT2: Alright it seems that some guys do not understand what is going on here and they say how can a null string be "null" in some conditions in Java? They find it riddiculus. I prefer them to try this on java:
The output will be "hellonull" Nothing else...
Also here we have a servlet. There is a null data. Servlet sends the null data as "null" what should it do? An empty string? Come on!!! "
看起来它返回字符串“null”而不是空对象。
Looks like it is return the String "null" and not a Null Object.
如果结果实际上是 null 就
足够了,但正如前面提到的,ID 的字符串值显然是“null”而不是 null 对象。
比较字符串时应该使用 .equals 而不是使用 ==
这个博客解释了更多关于他的信息:
http://blog.enrii.com/2006 /03/15/java-string-equality-common-mistake/
因此,在 Java 中,“null”不等于 null。
If the result was actually a null then
would suffice, but as is mentioned the string value of ID is obviously "null" rather than the null object.
You should use .equals when comparing strings rather than using ==
This blog explains more about his:
http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/
So, "null" does not equal null, in Java.
如果该值来自 Servlet,则容器很可能将空表单字段转换为空白字符串。您应该检查空值和空白 ("")。
或者,您可以使用 String 的 isEmpty() 方法:
If the value is coming from a Servlet then the container is most likely converting an empty form field to a blank string. You should do a check against null and blank ("").
Alternatively you can use String's
isEmpty()
method:如果变量 ID 的值是字符串“null”,那么我猜测当您使用 getParameter() 方法检索它时,代码中较早的部分存在错误。根据文档,如果指定名称没有值,则 getParameter() 方法应该返回 null (空引用)。这表明您正在某处执行将结果转换为字符串文字的操作,可能与空字符串连接(即 ID + "" ;)
If the value of your variable ID is the string literal "null", then I would guess that there is a bug earlier in the code when you retrieve it using the getParameter() method. According to the docs, the getParameter() method is supposed to return null (the null reference) if there is no value for the specified name. This indicates that somewhere you are doing an operation that converts the result to the string literal, perhaps concatinating with the empty string (i.e. ID + "" ; )