由于您的代码是当前编写的,
System.out.println(studentArray[0].getFirstName());
由于您永远不会初始化阵列,因此将抛出NPE。
private static students[] studentArray;
只声明它,不初始化它。
也许您应该按照以下方式重构代码
private static ArrayList<students> studentArray = new ArrayList<students>();// Inside the constructor or some method called before you access studentArray:studentArray.add(stu1);studentArray.add(stu2);//...// or, more concisely, since you're not using the temporary `students` variables:studentArray.add(new students(65435, "Bob", "Ted"));studentArray.add(new students(45546, "Guy", "Sid"));//...
如果您要使用数组而不是列表,
private static students[] studentArray;
需要用类似的东西初始化
private static students[] studentArray = new students[10];
然后您需要分配其元素
studentArray[0] = stu1;
要么
studentArray[0] = new students(65435, "Bob", "Ted");



