public class circleTest {
public static void main(String[] args) {
Student p=new Student();
p.cc(30);
}
}
class Student {
int numBer;//学号1-20
int state;//年级1-6
int score;//成绩0-100
public void cc(int i) {
Student arr[] = new Student[i];
for (int l = 0; l<arr.length; l++) {
arr[l].numBer = (int) (Math.random() * (20) + 1);
arr[l].state = (int) (Math.random() * (6) + 1);
arr[l].score = (int) (Math.random() * (101));
System.out.println("学号"+arr[l].numBer+" 年级"+arr[l].state+" 成绩"+arr[l].score);
}
}
}
报错
Cannot assign field "numBer" because "arr[l]" is null
对象数组赋值的方式错了。需要先创建对象,给对象赋值后,再赋值给对象数组。
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!