0

代码编译得很好,所以我认为我这样做是正确的,但是当我调用 toString 时,我从无参数构造函数中得到了一堆空值和零值,当我调用方法 totalScoresOfHomeworks 来查看它会返回什么值时,我得到了错误

"FException in thread "main" java.lang.NullPointerException
at GradeApplication.getTotalScoresOfHomeworks(Student.java:119)
at Student.main(Student.java:55)"

但这不是编译错误我使用“拆分”的方式可能有问题,也可能没有问题

这是我的代码:

import java.util.Scanner; 
import java.io.*;

public class Student
{
    public static void main(String[] args) throws IOException 
    {
        Scanner keyboard = new Scanner(System.in);

        //Define variables
        String name;
        int id;
        String homework;
        String labs;
        String tests;
        //int[] quizzesArray= new int[3];//**NO QUIZ GRADES
        double[] homeworkArray=new double[10];
        double[] labsArray= new double[6];
        double[] testsArray=new double[3];
        double project;
        double discussion;

        System.out.print("What is your name? ");
        name=keyboard.nextLine();

        System.out.print("\nWhat is your student ID? ");
        id=keyboard.nextInt();

        //Create strings to tokenize homework grades
        homework = keyboard.nextLine();
        System.out.println("\nPlease enter homework grades separated by spaces:");
        homework = keyboard.nextLine();
        //creat object to pass all info to class

        System.out.println("Please enter lab grades separated by spaces:");
        labs = keyboard.nextLine();

        System.out.println("Please enter test grades separated by spaces:");
        tests = keyboard.nextLine();

        System.out.println("Please enter project grade:");
        project = keyboard.nextDouble();

        System.out.println("Please enter discussion grade:");
        discussion = keyboard.nextDouble();

        System.out.println("\nResults: ");
        //Call toString method
        GradeApplication ga = new GradeApplication(name,id,homework,labs,tests,project,discussion);
        System.out.print(ga.toString());    

        System.out.print(ga.getTotalScoresOfHomeworks());
    }
}

class GradeApplication
{

    //Define variables
    String name;
    int id;
    String homework;
    String labs;
    String tests;
    double project;
    double discussion;

    //No argument constructor
    public GradeApplication()
    {
        name="";
        id=0;
        homework="";
        labs="";
        tests="";
        project=0.0;
        discussion=0.0;

    }

    //Parameter constructor
    public GradeApplication(String nme, int ID, String hw, String lbs, String tsts, double proj, double disc)
    {
        String name=nme;
        int id=ID;

        String homework=hw; 
        String labs=lbs;    
        String tests=tsts;

        double project=proj;
        double discussion=disc;
    }

    //Attributes
    private double totalScoresOfHomeworks;
    private double totalScoresOfLabs;
    private double totalScoresOfTests;
    private double scoreOfProject;
    private double scoreOfDiscussion;

    //Methods
    public double getTotalScoresOfHomeworks()
    {
        double[] homeworkArray=new double[10];
        double sum1=0;
        String[] tokens1;
        tokens1 = homework.split(" ");
                        for (int i=0;i<9; i++)
                        {
                            homeworkArray[i]=Double.parseDouble(tokens1[i]);
                            sum1+=homeworkArray[i];
                        }
        return sum1;
    }
    public void setTotalScoresOfHomeworks(double hwk)
    {
        totalScoresOfHomeworks=hwk;
    }

    public double getTotalScoresOfLabs()
    {
        double[] labsArray= new double[6];
        double sum2=0;
        String[] tokens2;
                    tokens2 = labs.split(" ");
                        for (int j=0;j<9; j++)
                            {
                                labsArray[j]=Double.parseDouble(tokens2[j]);    
                            }           
        return sum2;
    }
    public void setTotalScoresOfLabs(double labs)
    {
        totalScoresOfLabs=labs;
    }

    public double getTotalScoresOfTests()
    {
        double[] testsArray=new double[3];
        double sum3=0;
        String[] tokens3;
                {
                    tokens3 =tests.split(" ");
                        for (int k=0;k<9; k++)
                            {
                                testsArray[k]=Double.parseDouble(tokens3[k]);   
                            }                   
        return sum3;
    }
    public void setTotalScoresOfTests(double tests)
    {
        totalScoresOfTests=tests;
    }

    public double getScoreOfProject()
    {
        return project;
    }
    public void setScoreOfProject(double project)
    {
        scoreOfProject=project;
    }

    public double getscoresOfDiscussion()
    {
        return discussion;
    }
    public void setScoresOfDiscussion(double disc)
    {
        scoreOfDiscussion=disc;
    }

    public double getTotalScores()
    {
        return (totalScoresOfHomeworks+totalScoresOfLabs+totalScoresOfTests+scoreOfProject+scoreOfDiscussion);
    }

    public double getPercentage()
    {
        return 100*getTotalScores()/680;
    }

    public char getGrade()
    {
        char grade=0;
            if (getPercentage() >90 )
                grade='A';
            else if (getPercentage()>80)
                grade='B';
            else if (getPercentage()>70)
                grade='C';
            else if (getPercentage()>60)
                grade='D';
            else if (getPercentage() <= 60)
                grade='F';
        return grade;
    }

    public String toString()
    {
        String str="Student name:\t"+ name +
                        "\nStudent ID:\t" + id +
                        "\nTotal scores:\t" +getTotalScores()+
                        "\nMax scores:\t" + 680 +
                        "\nPercentage:\t"+ getPercentage() +
                        "\nGrade:\t" + getGrade();
        return str;
    }
}
4

4 回答 4

2

在您的构造函数中,您不需要再次声明变量

public GradeApplication(String nme, int ID, String hw, String lbs, String tsts, double proj, double disc)
    {
        name=nme;
        id=ID;

        homework=hw; 
        labs=lbs;    
        tests=tsts;

        project=proj;
        discussion=disc;
    }
于 2013-07-19T05:26:35.810 回答
1

在参数化构造函数中,您将参数分配给局部变量。您的所有类变量都不是由此设置的。因此抛出空指针异常。尝试将参数分配给实际的类变量。喜欢

public GradeApplication(String nme, int ID, String hw, String lbs, String tsts, double proj, double disc) {
    name = nme;
    id = ID;

    homework = hw;
    labs = lbs;
    tests = tsts;

    project = proj;
    discussion = disc;
} 
于 2013-07-19T05:43:04.663 回答
0

在作业变量中,它没有得到任何值,所以它不能拆分任何值。它是空的。tokens1 = homework.split(" ");所以你在这个方法中得到了 NullPointerException public double getTotalScoresOfHomeworks()

于 2013-07-19T05:44:49.680 回答
0

问题在于这段代码:

public double getTotalScoresOfHomeworks() {
        double[] homeworkArray = new double[10];
        double sum1 = 0;
        String[] tokens1;
        **tokens1 = homework.split(" ");**
        for (int i = 0; i < 9; i++) {
            homeworkArray[i] = Double.parseDouble(tokens1[i]);
            sum1 += homeworkArray[i];
        }
        return sum1;
    }

作业变量为空,您正在尝试拆分它。所以它正在产生错误。

第二个问题是

public GradeApplication(String nme, int ID, String hw, String lbs,
            String tsts, double proj, double disc) {
        String name = nme;
        int id = ID;

        String homework = hw;
        String labs = lbs;
        String tests = tsts;

        double project = proj;
        double discussion = disc;
    }

您再次声明这些变量,但在代码中您使用的是全局变量,因此所有变量均为空。从中删除声明部分。

一旦你解决了这个问题,你会再次遇到问题,

public double getTotalScoresOfHomeworks() {
        double[] homeworkArray = new double[10];
        double sum1 = 0;
        String[] tokens1;
        tokens1 = homework.split(" ");
        for (int i = 0; i < 9; i++) {
            homeworkArray[i] = Double.parseDouble(tokens1[i]);
            sum1 += homeworkArray[i];
        }
        return sum1;
    }

token1 是字符串数组,您正在尝试以双精度解析它。所以会导致运行时异常。

线程“main”java.lang.NumberFormatException 中的异常:对于输入字符串:“eng” at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at GradeApplication.getTotalScoresOfHomeworks(Student. java:107) 在 Student.main(Student.java:52)

彻底检查您的代码。

于 2013-07-19T05:51:52.587 回答