System.out.println( ">>>>>"+( (Integer)null == 0 ));
会导致 Exception in thread "main" java.lang.NullPointerException
因为null不属于任何对象,因此将null转化为Integer或者其他什么地方的时候就会发生这种情况。
一般情况是不会出现上面的情况的,但是有时候,某个方法
public Long allowedIn(String path) {
timeLastAccessed = System.currentTimeMillis();
if (!disallows.containsPrefixOf(path) || allows.containsPrefixOf(path)) {
return getDelayUntilNextAllowedCrawl(); // allowed after perhaps a non-0 suggested delay
} else {
//NullpointException
return null; // never allowed
}
}
返回null ,但是类型是基本类型的封装类。
然后又在另一个方法中返回
public Long allowedIn(WebURL webURL) {
if (!config.isEnabled()) {
return 0L;
}
try {
//....
return directives.allowedIn(path);
} catch (MalformedURLException e) {
logger.error(e);
}
return 0L;
}
最后就直接那这个值类作比较
robotstxtServer.allowedIn(webURL) == 0L
bingo,nullpointer。
因此一般对于这种返回null的方法最好返回默认值0,尽量避免传null
2、除了对象返回值,还会出现赋值未初始化的情况。
Course [] courses = new Course [2];
//Course是一个对,在这里我声明
//一个类的对象数组。
courses [0].setNum(1);
//调用set函数进行赋值
//出现错误
http://blog.csdn.net/ytdxyhz/article/details/15809271
3、尽量养成习惯,初始化对象,返回非null对象。