以NullPointerException结束

时间:2015-04-09 00:32:51

标签: java nullpointerexception

所以,我的任务是编写一个接受学生姓名和成绩的程序,并使用多个数组。我已经重写了以不同方式多次抛出运行时错误的代码段,但是出现了相同的NullPointerException错误。我已经运行测试将数组的内容打印到控制台,但还没有实际看到它返回“null”。所以我在这里迷路了...

错误:

Exception in thread "main" java.lang.NullPointerException
    at GradeBook.setScoreArray(GradeBook.java:41)
    at GradeBookApp.main(GradeBookApp.java:72)

GradeBookApp(主):

import java.util.Scanner;
import java.util.Arrays;

public class GradeBookApp
{
    public static void main(String[] args)
    {
        //constants
        final int NUM_STUDENTS = 2;
        final int NUM_SCORES = 4;

        //variables
        double myAverage;
        char myLetter;
        String myStudent;

        //arrays
        double[][] scoreArray = new double[NUM_STUDENTS][NUM_SCORES];
        String[] studentArray = new String[NUM_STUDENTS];


        //initialize scanner
        Scanner keyboard = new Scanner(System.in);

        for (int n = 0; n < NUM_STUDENTS; n++)
        {
            System.out.print("Enter the name of student #" + (n+1) + ": ");
            studentArray[n] = keyboard.nextLine();
        }
        System.out.println(" ");
        System.out.println(" ");

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

            //adds VALID individual test scores to the array
            for (int s = 0; s < NUM_SCORES; s++)
            {
                double scoreHolder = 0;

                do
                {
                    System.out.print("Enter test score #" + (s+1) + " for '" + studentArray[i] + "': ");
                    scoreHolder = keyboard.nextDouble();

                    if (scoreHolder > 100 || scoreHolder < 0)
                    {
                        System.out.println("***  SCORES MUST BE FROM 0-100 ONLY!  ***");
                    }

                } while(scoreHolder > 100 || scoreHolder < 0);

                scoreArray[i][s] = scoreHolder;
                //System.out.println(scoreHolder + " was added to position: " + i + "," + s);
            }
            System.out.println(" ");

        }

        System.out.println(Arrays.deepToString(scoreArray)); //This works as intended

        GradeBook myGradeBook = new GradeBook(NUM_SCORES, NUM_STUDENTS);
        for (int index = 0; index < NUM_STUDENTS; index++)
        {
            for (int score = 0; score < NUM_SCORES; score++)
            {
                myGradeBook.setScoreArray(scoreArray[index][score]);
            }
        }
        for (int student = 0; student < NUM_STUDENTS; student++)
        {
            myGradeBook.setStudentArray(studentArray[student]);
        }

        //for each student...
        for (int i = 0; i < NUM_STUDENTS; i++)
        {
            //calculate average

            myAverage = myGradeBook.getGradeAvg(i);
            //calculate letter grade

            myLetter = myGradeBook.getLetterGrade(myAverage);
            //get student name for display

            myStudent = myGradeBook.getStudentName(i);

            //display results
            System.out.println(myStudent + "'s final score is: " + myAverage + " (" + myLetter + ")");
        }

    }
}

的GradeBook(类):

public class GradeBook {

    private String[] studentNames;
    private char[] letterGrades = { 'A', 'B', 'C', 'D', 'F' };
    private double[][] studentScores;
    private int numStudents;
    private int numScores;


    public GradeBook(int NUM_SCORES, int NUM_STUDENTS)
    {
        numStudents = NUM_STUDENTS;
        numScores = NUM_SCORES;
    }


    public void setStudentArray(String students)
    {
        //set the student array
        for (int i = 0; i < numStudents; i++)
        {
            studentNames[i] = students;
        }
    }

    public void setScoreArray(double scores)
    {
        //for each student...
        for (int i = 0; i < numStudents; i++)
        {
            //set each individual score
            for (int s = 0; s < numScores; s++)
            {
                studentScores[i][s] = scores;
            }
        }
    }

    public String getStudentName(int pos)
    {
        return studentNames[pos];
    }

    public double getGradeAvg(int pos)
    {
        double total = 0;
        double avg = 0;

        //use the position variable to iterate over the correct test scores...
        for (int i = 0; i < numScores; i++)
        {
            total += studentScores[pos][i];
        }

        avg = total / numScores;
        return avg;
    }

    public char getLetterGrade(double average)
    {
        char letter = 'Z';

        if (average >= 0 && average < 60)
        {
            letter = letterGrades[4];
        }
        else if (average > 60 && average < 70)
        {
            letter = letterGrades[3];
        }
        else if (average > 70 && average < 80)
        {
            letter = letterGrades[2];
        }
        else if (average > 80 && average < 90)
        {
            letter = letterGrades[1];
        }
        else if (average > 90 && average <= 100)
        {
            letter = letterGrades[0];
        }

        return letter;
    }



}

我能找到的每件事都说我引用了一个空值?但我已经通过循环手动将所有分数设置为90,并且仍然得到相同的结果。我迷失在哪里,离开这里。

非常感谢任何帮助。感谢。

编辑:为了澄清,我添加了整个代码,而不是代码段。此外,我还添加了Arrays.toDeepString来测试数组,这些值按照预期打印到控制台。

1 个答案:

答案 0 :(得分:0)

您使用studentScores声明private double[][] studentScores;,但您不会在任何地方进行初始化。它是一个空指针,直到你初始化它。当您知道其尺寸时,可以在构造函数中执行此操作:

studentScores = new double[numStudents][numScores];