字符串说它不为空,但后来抛出 NullPointerException [英] String says its not null but then later throw NullPointerException

查看:67
本文介绍了字符串说它不为空,但后来抛出 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

天哪.我有一个小项目要做,弦乐让我很伤心!

OMG. I have a little project to do and the Strings are killing me!

现在,我有一个 null 字符串(取自从 servlet 调用 getParameter() 的值).

Now, I have a String which is null (is taken the value from invoking getParameter() from a servlet).

问题是,我正在尝试查看它是否为空,并且即使它为空,程序中也告诉我这不是 null,但稍后在程序中,当我正在使用该变量时,我收到一个异常,指出该变量为 null.

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);

我做错了什么?

只有第四个条件有效!剩下的呢(除了第二个,因为我把它放在了因为我很绝望)

Only the forth condition is working! What about the rest(except second one, because that condition i put it because I was desperate)

我教过 ID == nullID.equals(null) 可以,但是不行.

I taught ID == null or ID.equals(null) will be ok, but no.

问题是,我从表单中获取 ID 的值(我们说通常是表单 1).但在这种情况下,我使用的是没有任何 ID 输入的表单 2,因此 ID 必须是 null 而不是 "null"

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"

推荐答案

既然你从一个 servlet 得到字符串,我可以说这是正常的.

Since you get the string from a servlet i can say that this is normal.

Java 在某些条件下将空字符串转换为空"字符串.

Java converts a null string to a "null" string on some conditions.

显然你检索的字符串不是空值,而是一个 4 个字符的字符串空"

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:

System.out.println("Length of ID:  " + ID.Length);

如果你在这里没有得到异常,这意味着该字符串不为空,并且输出Length of ID: 4"将意味着该字符串实际上是 ID = "null"

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"

好吧,似乎有些人不明白这里发生了什么,他们说在 Java 的某些条件下,空字符串如何为空"?他们觉得这很荒谬.我更喜欢他们在 java 上尝试这个:

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:

String abc = null;
String xyz = "hello"; 
System.out.println(xyz + abc); 

输出将是 "hellonull" 没有别的...

The output will be "hellonull" Nothing else...

这里还有一个 servlet.有一个空数据.Servlet 将空数据发送为空"它应该怎么做?空字符串?来吧!!!"

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!!! "

这篇关于字符串说它不为空,但后来抛出 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆