java.lang.NullPointerException

发布于 2024-09-04 11:20:11 字数 138 浏览 3 评论 0原文

我对一个值为空和不为空的值执行一个条件。 当这个值为空时,我得到了java.lang.NullPointerException

我该如何处理这个问题才能消除这个异常?

我需要值何时为空以及何时不为空的条件。

I do a condition with a value when it is null and when it is not null.
The time where this value is null I got the java.lang.NullPointerException.

How could I deal with this in order to dismiss this exception?

I need the condition when the value is null and when it is not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

亢潮 2024-09-11 11:20:11

当您尝试使用 null 变量调用方法时,您会收到 NullPointerException。简单的例子:

String s = null;
int len = s.length();  // NullPointerException because s is null

因此,在调用变量的任何方法之前,您应该检查变量是否为 null,例如:

int len;
if (s == null) {
    len = 0;
}
else {
    len = s.length();  // Safe, s is never null when you get here
}

请注意,NullPointerException 通常很容易解决。仔细查看异常的堆栈跟踪;它准确地告诉您异常发生在代码的哪一行。检查该行代码中哪些内容可能为 null,并检查您是否正在对可能为 null 的内容调用方法。添加一个检查(或防止相关事物以另一种方式为 null)。

You get a NullPointerException when you try to call a method using a variable that is null. Simple example:

String s = null;
int len = s.length();  // NullPointerException because s is null

So you should check if the variable is null before calling any method on it, for example:

int len;
if (s == null) {
    len = 0;
}
else {
    len = s.length();  // Safe, s is never null when you get here
}

Note that a NullPointerException is usually easy to solve. Carefully look at the stack trace of the exception; it tells you exactly in which line of your code the exception happens. Check what could be null in that line of code, and check if you're calling a method on something that might be null. Add a check (or prevent that the relevant thing can ever be null in another way).

橘虞初梦 2024-09-11 11:20:11

只需正确检查值是否为 null。诸如之类的条件

if (value == null)

永远不会引发 NullRefePointerException。仅当您尝试访问 null 引用指向的任何内容时,才会出现异常。因此调用方法或访问对象的其他实例成员是不可能的。

Simply do a proper check for the value being null. A conditional such as

if (value == null)

will never raise a NullRefePointerException. It's only if you try accessing whatever a null reference points to that you get the exception. So calling methods or accessing other instance members of the object is out of the question.

雪若未夕 2024-09-11 11:20:11

使用 if 检查变量是否不为空,然后使用 else 代码块检查变量是否为空。

if (variable != null)
{
    // Code when object is not null
}
else
{
    // Code when object is null
}

仅当您尝试使用空值时才会抛出空指针。

例如,

 variable.method();

但是您应该始终避免可能出现空指针的情况。

Use an if to check for the variable not being null and then use an else code block for when it is null.

if (variable != null)
{
    // Code when object is not null
}
else
{
    // Code when object is null
}

The null pointer is only thrown when you try and use the null value.

For example,

 variable.method();

But you should always avoid a situation where a null pointer could occur.

日久见人心 2024-09-11 11:20:11

正如其他发帖者所述,如果您不希望此处的 s 引用为空,那么首先修复导致引用为空的错误。但是,如果引用可以为 null 是有效的,您还可以使用三元运算符(Java 5 上)执行以下操作。

int len = (s == null) ? 0 : s.length;

注意:括号是可选的,但在我看来,它们使其更具可读性。

As stated by other posters, if you do not expect the s reference to be null here then fix the bug that causes the reference to be null in the first place. However, if it is valid that the reference could be null, you can also do the following using the ternary operator (Java 5 on)

int len = (s == null) ? 0 : s.length;

Note: the brackets are optional, but they make it a bit more readable in my opinion.

或十年 2024-09-11 11:20:11

这是一个非常令人困惑的问题,但是:

boolean isNull = true;
if (value != null) {
    isNull = false;
}

It is a really confusing question, but:

boolean isNull = true;
if (value != null) {
    isNull = false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文