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
数组不能赋值,不知道为什么空值
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-sinJack 2022-03-06 11:29关注
对象数组赋值的方式错了。需要先创建对象,给对象赋值后,再赋值给对象数组。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报