public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Students1[] stus = new Students1[3];
Students1Biz biz = new Students1Biz();
Students1 stu1 = new Students1();
stu1.score = 96;
Students1 stu2 = new Students1();
stu2.score = 56;
Students1 stu3 = new Students1();
stu3.score = 78;
stus[0] = stu1;
stus[1] = stu2;
stus[2] = stu3;
/*
for (int i = 0; i < stus.length; i++) {
System.out.println("请输入" + (i + 1) + "个学员的成绩:");
stu[i].score = scanner.nextInt();
}
*/
biz.seaScore(stus);
}
为什么stu[i].score = scanner.nextInt();
不能接受输入数据 报空指针异常
空指针异常就是对象为空
java一切都是对象,比如,字符串有个length()方法,
但是,null是没length方法的,你String.length()是没问题的,但是,null.length()那就肯定有问题,这就是空指针异常
你stu是哪里来的?
stus才对吧.
应该是stus[i].score = scanner.nextInt();
for循环里面的那个集合写错了吧stus[i].score = scanner.nextInt();
空指针异常就是对象为空,你的那个stu[i]为空null,应该是stus[i].score = scanner.nextInt();
eg: String str = new String(); str.equals("hello");这是可以的
但是 String str = null;str.equals("hello");这就会出现空指针异常;因为null在内存中没有任何空间,它也就没有任何方法和属性
就是你其中有某个对象的值是空
不知道stu是哪里来的,没定义的变量按理说使用是会报错的啊。调试下执行到for的时候stus的内容是不是为空
stus[i].score = scanner.nextInt();
for 循环集合写错了
for (int i = 0; i < stus.length; i++) {
System.out.println("请输入" + (i + 1) + "个学员的成绩:");
stu[i].score = scanner.nextInt();
}
*/
biz.seaScore(stus);
循环内的 stu[i]改成stus[i].score
stu[i].score = scanner.nextInt(); 写错了吧
打个断点看看,,什么为空
biz.seaScore(stus); 这里应该是set吧 biz.setScore(stus);
还有这里stu[i].score = scanner.nextInt(); 应该是stus[i].score = scanner.nextInt();