我无法测试我的程序:"java.lang.NullPointerException"

I can't get my program to test: "java.lang.NullPointerException"

当我尝试 运行 程序时,这就是计算机 returns:

First_Name Last_Name Test1 Test2 Test3 Test4 Test5 Average Grade

Exception in thread "main" java.lang.NullPointerException at tests.Tests.main(Tests.java:132) C:\Users\Michael Jr\AppData\Local\NetBeans\Cache.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

package tests;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author Michael 
 */
public class Tests {

   private String fname;
    private String lname;
    private int[] scores;
 
    public Tests(String entry)
    {
        String[] data=entry.split(" ");
        scores=new int[data.length-2];
        this.setFname(data[0]);
        this.setLname(data[1]);
        for(int i=2;i<data.length;i++)
        {
            scores[i-2]=Integer.parseInt(data[i]);
        }
    }
    public int[] getScores() {
        return scores;
    }
    public void setScores(int[] scores) {
        this.scores = scores;
    }
 
    public static String getAverageScore(int[] a)
    {
        int sum=0;
        for(int e:a)
        {
            sum+=e;
        }
        return new DecimalFormat("##.##").format((double)sum/(double)a.length);
    }
 
    public static void showAverage(List<Tests> e)
    {
        for(Tests s:e)
        {
            System.out.println(s);
        }
    }
    public String getGrade()
    {
        double d=Double.parseDouble(Tests.getAverageScore(scores));
        if(d<=100 && d>=90)
        {
            return "A";
        }
        else if(d<90 && d>=80)
        {
            return "B";
        }
        else if(d<80 && d>=70)
        {
            return "C";
        }
        else if(d<70 && d>=60)
        {
            return "D";
        }
        else
        {
            return "F";
        }
    }
    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 toString()
    {
        return getFname()+"\t\t"+getLname()+"\t\t"+scores[0]+"\t\t"+scores[1]+"\t\t"+scores[2]+"\t\t"+scores[3]+"\t\t"+scores[4]+"\t\t"+getAverageScore(scores)+"\t\t"+getGrade();
    }
 
    public static ArrayList<Tests> read(String path)
    {
        BufferedReader br = null;
        try {
 
            String sCurrentLine;
            ArrayList<Tests> ar=new ArrayList<Tests>();
            br = new BufferedReader(new FileReader("C:\Users\Michael Jr\Desktop\ch9_Ex13Data.txt"));
 
            while ((sCurrentLine = br.readLine()) != null) 
            {
                ar.add(new Tests(sCurrentLine));
            }
            return ar;
 
        } 
        catch (Exception e) 
        {
 
        } 
        finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }
    public static void main(String[] a)
    {
        ArrayList<Tests> ar=read("E:/score.txt");
        System.out.println("First_Name\tLast_Name\tTest1\t\tTest2\t\tTest3\t\tTest4\t\tTest5\t\tAverage\t\tGrade");
        for(Tests e:ar)
        {
            System.out.println(e);
        }
    }
}
    

您应该检查读取函数的结果:

public static void main(String[] a)
    {
        ArrayList<Tests> ar=read("E:/score.txt");
        if (ar != null){  
            System.out.println("First_Name\tLast_Name\tTest1\t\tTest2\t\tTest3\t\tTest4 \t\tTest5\t\tAverage\t\tGrade");
            for(Tests e:ar)
            {
                System.out.println(e);
            }
        } else {
            System.out.println("Could not read file");
        }
    }

您在验证 try 语句吗?读取函数正在返回 'Null' 值。我认为这个问题是在您读取文件时出现的。

String sCurrentLine;
        ArrayList<Tests> ar=new ArrayList<Tests>();
        br = new BufferedReader(new FileReader("C:\Users\Michael Jr\Desktop\ch9_Ex13Data.txt"));

        while ((sCurrentLine = br.readLine()) != null) 
        {
            ar.add(new Tests(sCurrentLine));
        }
        return ar;

如果你能分享文件,我会验证这个代码正确!