我正在开发一些java项目。 在这里,我被困在这个问题,无法找出我要去哪里错了。空指针异常
我做了两个类:Test
和Child
。
当我运行代码时,我得到一个NullPointerException。
package com.test;
public class Test {
child newchild = new child();
public static void main(String[] args) {
new Test().method();
}
void method() {
String[] b;
b = newchild.main();
int i = 0;
while (i < b.length) {
System.out.println(b[i]);
}
}
}
package com.test;
public class child {
public String[] main() {
String[] a = null;
a[0] = "This";
a[1] = "is";
a[2] = "not";
a[3] = "working";
return a;
}
}
未来(甚至现在),你应该清楚地表明该行被抛出异常。该信息在异常的堆栈跟踪中可用。 –
...它按预期工作:它在'child#main()'中创建'NullPointerException' ;-) –