java - 显示数组时数组元素给出 null 和 0

标签 java arrays null zero

这个程序应该存储通过键盘输入的输入(名称,id,标记)并将它们存储在数组(不是数组列表)中,然后在调用时显示。

但它给了我这个:

Student [studentName=nullnull, studentID=null, project 1 marks =0, ICT marks=0, Active P= 0, Code Assg.=0, ICT2= 0, Active P=0, ICT3 =0, Active P=0 ,]

Student [studentName=nullnull, studentID=null, project 1 marks =0, ICT marks=0, Active P= 0, Code Assg.=0, ICT2= 0, Active P=0, ICT3 =0, Active P=0 ,]

Exception in thread "main" java.lang.NullPointerException
    at Main.sortStudentsUsingName(Main.java:164)
    at Main.main(Main.java:151)
Student [studentName=nullnull, studentID=null, project 1 marks =0, ICT marks=0, Active P= 0, Code Assg.=0, ICT2= 0, Active P=0, ICT3 =0, Active P=0 ,]
Java Result: 1   

这是我的两个类(class)(最初不是我的。已编辑)

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);// for input
                System.out.println("Input no. of Student");
        int x = sc.nextInt();
                Student []array = new Student[x];// create array with size of 5

        for (int i = 0; i <x; i++){
            System.out.println("Input Student Details "+i);

            String fName = sc.next(); 

            String stuID = sc.next();                    

                        Student newstudent = new Student();
            int projM1;
            do {
                System.out.println("Please enter the projM1 Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next();
                }

                projM1 = sc.nextInt();
            } while (projM1 < 0 || projM1 > 100);

            int ictM1;

            do {
                System.out.println("Please enter the ICT1 Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                ictM1 = sc.nextInt();
            } while (ictM1 < 0 || ictM1 > 100);

            int actP1;

            do {
                System.out.println("Please enter the actP1 Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                actP1 = sc.nextInt();
            } while (actP1 < 0 || actP1 > 100);

                         int codeAsM;

            do {
                System.out.println("Please enter the codeAsM Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                codeAsM = sc.nextInt();
            } while (codeAsM < 0 || codeAsM > 100); 

                         int ictM2;

            do {
                System.out.println("Please enter the ictM2 Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                ictM2 = sc.nextInt();
            } while (ictM2 < 0 || ictM2 > 100);

                         int actP2;

            do {
                System.out.println("Please enter the actP2 Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                actP2 = sc.nextInt();
            } while (actP2 < 0 || actP2 > 100);

                         int ictM3;

            do {
                System.out.println("Please enter the ictM3 Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                ictM3 = sc.nextInt();
            } while (ictM3 < 0 || ictM3 > 100);

                        int actP3;

            do {
                System.out.println("Please enter the actP3 Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                actP3 = sc.nextInt();
            } while (actP3 < 0 || actP3 > 100);

                        /* int component1Marks;

            do {
                System.out.println("Please enter the component1Marks Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                component1Marks = sc.nextInt();
            } while (component1Marks < 0 || component1Marks > 100);

                        int component2Marks;

            do {
                System.out.println("Please enter the component2Marks Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                component2Marks = sc.nextInt();
            } while (component2Marks < 0 || component2Marks > 100);

                        int component3Marks;

            do {
                System.out.println("Please enter the component3Marks Marks!");
                while (!sc.hasNextInt()) {
                    System.out.println("That's not a number!");
                    sc.next(); // this is important!
                }
                component3Marks = sc.nextInt();
            } while (component3Marks < 0 || component3Marks > 100); */

            array[i] = new Student();


        }

        for(Student newstudent : array){
            System.out.println(newstudent);
        }

        sortStudentsUsingName(array); // sort method is called

        for(Student newstudent : array){
            System.out.println(newstudent);
        }       
    }

    public static void sortStudentsUsingName(Student []array){
        for (int i = 0; i < array.length; i++){
            Student min = array[i];

            int minIndex = i;
            for (int j = i + 1; j < array.length; j++){
                if (min.getfName().compareTo(array[j].getfName()) > 0){ 
                    // How compareTo method works
                    //a.compareTo(b) == 0 means both a and b are equal
                    //a.compareTo(b) > 0 means a is large and b is small
                    //a.compareTo(b) < 0 means b is large and a is small
                    //for Descending order change the conditions to: 
                    //min.getStudentName().compareTo(array[j].getStudentName()) < 0
                    min = array[j];
                    minIndex = j;
                }
            }
            if (i != minIndex){
                Student temp = array[minIndex];
                array[minIndex] = array[i];
                array[i] = temp;
            }
        }
    }
}

这是学生类

/**
 * @author Guhanathan Poravi
 *
 */
public class Student {

    private  String fName;
    private  String lName;
    private  String stuID;

    private  int projM1;
    private  int ictM1;
    private  int actP1;


    private  int codeAsM;
    private  int ictM2;
    private  int actP2;


    private  int ictM3;
    private  int actP3;

    private  double component1Marks = (projM1 * 0.3 +  ictM1 * 0.6 +  actP1 * 0.1) ;
    private  double component2Marks = (codeAsM *  0.5 +  ictM2 * 0.4  + actP2 * 0.1);
    private  double component3Marks = (ictM3 *0.9  +  actP3 * 0.1);
    private  double moduleMarks =(component1Marks + component2Marks + component3Marks);
    private  String fGrade; 

    public Student(/*String fName, String stuID, int projM1, int ictM1,
            int actP1, int codeAsM, int ictM2,
            int actP2, int ictM3, int actP3,double component1Marks,double component2Marks, double component3Marks, double moduleMarks */) {
        super();
    this.fName = fName;
        this.lName = lName;
        this.stuID = stuID;
        this.projM1 = projM1;
        this.ictM1 = ictM1;
        this.actP1 = actP1;
        this.codeAsM = codeAsM;
        this.ictM2 = ictM2;
        this.actP2 = actP2;
        this.ictM3 = ictM3;
        this.actP3 = actP3;
        this.component1Marks = (projM1 * 0.3 +  ictM1 * 0.6 +  actP1 * 0.1) ;
        this.component2Marks = (codeAsM *  0.5 +  ictM2 * 0.4  + actP2 * 0.1);
        this.component3Marks = (ictM3 *0.9  +  actP3 * 0.1);
        this.moduleMarks =  (component1Marks + component2Marks + component3Marks);


    }
    public String getfName() {
            return fName;
        }

        public void setfName(String fName) {
            this.fName = fName;
        }

        public String getlName() {
            return lName;
        }

        public void setlName(String lName) {
            this.lName = lName;
        }

        public String getStuID() {
            return stuID;
        }

        public void setStuID(String stuID) {
            this.stuID = stuID;
        }

        public int getProjM1() {
            return projM1;
        }

        public void setProjM1(int projM1) {
            this.projM1 = projM1;
        }

        public int getIctM1() {
            return ictM1;
        }

        public void setIctM1(int ictM1) {
            this.ictM1 = ictM1;
        }

        public int getActP1() {
            return actP1;
        }

        public void setActP1(int actP1) {
            this.actP1 = actP1;
        }

        public int getCodeAsM() {
            return codeAsM;
        }

        public void setCodeAsM(int codeAsM) {
            this.codeAsM = codeAsM;
        }

        public int getIctM2() {
            return ictM2;
        }

        public void setIctM2(int ictM2) {
            this.ictM2 = ictM2;
        }

        public int getActP2() {
            return actP2;
        }

        public void setActP2(int actP2) {
            this.actP2 = actP2;
        }

        public int getIctM3() {
            return ictM3;
        }

        public void setIctM3(int ictM3) {
            this.ictM3 = ictM3;
        }

        public int getActP3() {
            return actP3;
        }

        public void setActP3(int actP3) {
            this.actP3 = actP3;
        }

        public double getComponent1Marks() {
            return component1Marks;
        }

        public void setComponent1Marks(int component1Marks) {
            this.component1Marks = component1Marks;
        }

        public double getComponent2Marks() {
            return component2Marks;
        }

        public void setComponent2Marks(int component2Marks) {
            this.component2Marks = component2Marks;
        }

        public double getComponent3Marks() {
            return component3Marks;
        }

        public void setComponent3Marks(int component3Marks) {
            this.component3Marks = component3Marks;
        }

        public double getModuleMarks() {
            return moduleMarks;
        }

        public void setModuleMarks(double moduleMarks) {
            this.moduleMarks = moduleMarks;
        }


    @Override
    public String toString() {
        return "Student [studentName=" + fName+lName + ", studentID="
                + stuID + ", project 1 marks =" + projM1 + ", ICT marks=" + ictM1 +
                         ", Active P= " + actP1 + ", Code Assg.=" + codeAsM +","
                        + " ICT2= " + ictM2 +", Active P=" + actP2 +", ICT3 =" + ictM3
                        + ", Active P=" + actP3 +" ,]";
    }


}

我该如何纠正这个问题?我的输入是否进入数组?如果是这样,为什么它给我这个输出?

最佳答案

您正在使用空的 Student 实例填充数组(使用无参数构造函数创建,并且稍后不会设置它们的任何属性)。这就是为什么它们打印时所有属性都具有默认值(0 和 null)。

发生 NullPointerException 是因为您尝试按学生姓名对数组进行排序,而所有姓名均为 null。

此代码似乎与您的 previous question 相关,您可以在创建每个 Student 实例后设置 Student 属性。如果您希望属性具有值,则应该设置它们。

关于java - 显示数组时数组元素给出 null 和 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26780643/

相关文章:

java - 有没有办法编写将具有相似参数的许多函数分组?

java - 整数太长 Java

Java正则表达式如何找到父匹配项?

javascript - 如何保持 Javascript 数组排序,而不对其进行排序

javascript - 比较数组(也可以是二维及以上)

C 动态分配数组的问题

java - (JAVA)初始化对象——一行调用方法

java - IntelliJ - 是否可以在 watch 中使用循环?或者在自定义类渲染器中?

java - 如何与空字节进行比较?

mysql - mysql 5.0 是否索引空值?