【发布时间】:2017-01-10 07:09:40
【问题描述】:
public class Wells {
String str[] = null;
static public void main(String args[]) {
Wells objWells = new Wells();
System.out.println(args.length);// we will NOT get null pointer exception WHY ?
System.out.println(objWells.str.length); // we will get null pointer exception
}
}
【问题讨论】:
-
新 Wells 的
str[]为空,因此当您尝试获取其长度时会得到 NPE。对于“为什么argsnot null”部分,请参阅stackoverflow.com/questions/9605532/… -
问题是,你为什么期望
str不是null。至于args,绝对不会默认的