组类中的NullPointerException。找不到数组对象

时间:2017-03-25 05:24:17

标签: java eclipse nullpointerexception

因此,当我尝试运行我的Group类时,它告诉我学生数组的内容为空,我无法找到具体的位置(过去4个小时都在尝试计算出来)。如果您有任何疑问关于课程,不要犹豫。

提前感谢您的所有答案。

小组课程

public class Group {

        private Student studentArray[];
        private int nbOfStudent;

        public Group() {
            studentArray[]= new Student[24];
        }

        public void add( Student stud){
            studentArray[nbOfStudent]=stud;
            nbOfStudent++;
        }

        public String toString(){
            String msg="";

            for(int i=0;i<studentArray.length;i++){
                msg+=tabStudent[i]+" ";
            }

            return msg;
        }

        public Student getStudentArray(int i) {
            return studentArray[i];
        }

        public Student[] getstudentArray(){
            return studentArray;
        }

        public void setStudentArray[](Student[] studentArray) {
            this.studentArray= studentArray;
        }

        public int getNbOfStudent() {
            return nbOfStudent;
        }

        public void setNbOfStudent(int student) {
            this.nbOfStudent = student;
        }
        public int search(String code){
            return UtilsTabs.search(studentArray, code,this.nbOfStudent);
        }

        public void sort(){
         UtilsTabs.sort(studentArray, nbOfStudent);
        }
    }

UtilsTabs类

public static void sort(Student[] array, int nbOfStud){
    Student temp=null;
    int minIndex=0;

    for(int i=0;i<array.length-1;i++){
        minIndex=i;

        for(int j=i+1;j<nbOfStud;j++){
            int  comparedValue = tab[j].compareTo(tab[minIndex]);
            if( comparedValue< 0){
                minIndex=j;
            }
        }
        temp=tab[i];
        array[i]=array[minIndex];
        array[minIndex]=temp;
    }
}
public static int search(Student array[],String code,int nbofStud){

    int pos=-1;

    for(int i=0;i<tarray.length;i++){

        if(tab[i].getCode().equalsIgnoreCase(code)); 

            pos=i;
    }

    return pos;
}

学生班

public class Student {
    private String code;
    private String name;
    private Grades evaluation;


    public Student(String code, String name, String eval) {
        this.code = code;
        this.nom = nom;
        this.evaluation=new Grades(eval);
    }
    public Message message(){
        if(this.evaluation.gradeAverage()<60)
            return Message.FAILED;

        else
            return Message.SUCCESS;
    }

    public boolean equals(Student other){
        boolean res=this.code.equals(other.code);
        return res;

    }

    public int compareTo(Student other){
        int res= this.code.compareTo(other.code);
        return res;

    }

}

班组测试

Group gr = new Group();

Student stud2 = new Student("26161234", "Marc", "65 81 58 100 79");
gr.add(stud2);

Student stud=new Student("24910003", "Pierre", "45 59 36 66");
gr.add(stud);

//show group of student
System.out.println("Group of students:\n" + gr.toString());

这是控制台向我显示的内容

result of test of Group class

1 个答案:

答案 0 :(得分:0)

你的group.toString正在使用tabStudent我无法看到初始化或在其他任何地方使用