String s=forecast.getStatus();
就是这么简单的一行代码,forecast是我自己建的一个bean,public class Forecast
里面都是简单的set和get。
现在因为我没有进行set,所以这个Status应该是默认的null才对,获取null还会报空指针错误?
IDE的错误报告:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.leu.littleweather.bean.Forecast.getStatus()' on a null object reference
神奇的是有时候它又不会报错,有时候会报错。。。真是日了狗了!
getStatus() on a null object reference
means that forecast is null and you cannot call methods on null objects. So you need to check what causes forecast to be null sometimes and not null sometimes.It means your forecast is empty. .
If the object is empty, of course a null pointer is reported.
Just like if you don’t have a girlfriend, do you still expect to adjust your girlfriend’s API? If you want to adjust it, do you need to buy a girlfriend first?