【发布时间】:2017-05-03 10:56:02
【问题描述】:
当我在 eclipse 中执行以下代码时:
class Account
{
int a, b;
public void setData(int c, int d)
{
a=c;
b=d;
}
public void showData()
{
System.out.println("Value of a: "+a);
System.out.println("Value of b: "+b);
}
}
public class ObjectArray
{
public static void main(String args[])
{
Account obj[] = new Account[2];
//obj[0]=new Account();
//obj[1]=new Account();
obj[0].setData(1, 2);
obj[1].setData(3, 4);
System.out.println("For Array Element 0:");
obj[0].showData();
System.out.println("For Array Element 1:");
obj[1].showData();
}
}
我收到以下异常:
线程“main”中的异常 java.lang.NullPointerException 在 ObjectArray.main(ObjectArray.java:24)
请给我建议为什么会发生这个错误?
【问题讨论】:
-
因为您确实注释掉了会阻止
NPE的初始化?
标签: java arrays eclipse nullpointerexception