空指针异常 [英] Nullpointer exception

查看:109
本文介绍了空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能这可能是一个重复的问题。
我将一个String变量初始化为null。我可能会或可能不会用value更新它。现在我想检查这个变量是否不等于null,无论我尝试什么,我得到一个空指针异常。我可以'因为它很昂贵,所以可以抛出nullpointer异常。是否有任何有效的解决方法.TIA

There is a possiblity that this may be a dupicate question. I initialize a String variable to null.I may or may not update it with a value.Now I want to check whether this variable is not equal to null and whatever I try I get a null pointer exception.I can't afford to throw nullpointer exception as it is costly.Is there any workaround that is efficient.TIA

推荐答案

如果你使用

if (x == null)

你将获得 NullPointerException

我怀疑你在做:

if (x.y == null)

因为 x 为空而抛出,不是因为 xy 为空。

which is throwing because x is null, not because x.y is null.

如果无法解释,请发布您用来测试无效的代码。

If that doesn't explain it, please post the code you're using to test for nullity.

这篇关于空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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