【发布时间】:2016-10-26 06:57:49
【问题描述】:
这是我遇到错误的地方
Student[] student = new Student[10];
System.out.println(" Test : " + student[2].RollNo);
但是当我没有得到错误时
Student student = new Student();
System.out.println(" Test : " + student.RollNo);
只有在创建对象数组时才会出现错误。
【问题讨论】:
-
因为你还没有为你的数组分配任何对象
-
student[2]是null -
调用“new array”创建了数组,但它的所有槽最初都是空的。
-
@AndrewTobilko 但如何?如何不让它为空?
-
@user3861350
student[2] = new Student();(或循环执行)
标签: java arrays class object nullpointerexception