小白求助,空指针报错是什么原因

public class StudentTest01 {
    public static void main(String[] args) {
        Student1[] stu = new Student1[20];
        for (int i = 0; i < stu.length; i++) {
            //给数组元素赋值,创建对象
            stu[i] = new Student1();
            //给数组元素中的属性赋值
            stu[i].number = i + 1;
            //年级:1-6
            stu[i].state = (int) (Math.random() * 6 + 1);
            //成绩: 0-100
            stu[i].score = (int) (Math.random() * 100 + 1);
            StudentTest01 test = new StudentTest01();

            test.print(stu);



        }
    }
    //排序后输出
    public void scoreSort(Student1[] stu){
        for(int i = 0; i < stu.length - 1;i++){
            for(int j = 0; j < stu.length - i -1;j++){
                if(stu[j].score > stu[j + 1].score){
                    //若需要换序,必须交换数组的元素,不是数组元素的属性!!!!
                    Student1 temp = stu[j];
                    stu[j] = stu[j + 1];
                    stu[j + 1] = temp;
                }
            }
        }
        print(stu);

    }

    //查找指定年级的学生信息
    public void searchState(Student1[] stu,int state){
        for(int i = 0; i < stu.length;i++){
            if(stu[i].state == 3){
                stu[i].info();
            }
        }
    }

    //遍历学生信息的方法
    public void print(Student1[] stu){
        for(int i = 0;i < stu.length;i++){
            stu[i].info();
        }
    }
}

 class Student1{
    int number;
    int state;
    int score;
     //显示学生信息
     public void info(){
         System.out.println("学号是:" + number+" "+ "年级是:"+ state + " " + "成绩是:" + score);

    }
}

因为你 test.print(stu);写在循环里
在i=0的时候就会调用,但是这个时候stu中只有1个元素被赋值了
所以报空指针
把test.print(stu);写在循环执行结束后的地方就行了