-1

When I try and run the program this is what the computer 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\8.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);
        }
    }
}
    
Community
  • 1
  • 1
mikeg4523
  • 21
  • 6

2 Answers2

0

You should check the result of your read function:

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");
        }
    }
markt
  • 895
  • 7
  • 20
  • Or make the read method always return a list instead of null – OneCricketeer Apr 30 '16 at 00:32
  • this fixed the null problem but the file can't be read now? and i have the location in just right. – mikeg4523 May 01 '16 at 23:43
  • You are passing in a value of "E:/score.txt" as path, but then reading from a hard-coded path of "C:\\Users\\Michael Jr\\Desktop\\ch9_Ex13Data.txt" - which file do you want to read? Maybe this is a permissions issue? – markt May 02 '16 at 22:36
0

are you verifying the try statement?, the read function is returning 'Null' value. I think this problem is when you are reading the file.

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;

if you are able to share the file, I will verify this code right!

  • the contents of the file the program is trying to read are below. – mikeg4523 Apr 30 '16 at 07:22
  • Jack Johnson 85 83 77 91 76 Lisa Aniston 80 90 95 93 48 Andy Cooper 78 81 11 90 73 Ravi Gupta 92 83 30 69 87 Bonny Blair 23 45 96 38 59 Danny Clark 60 85 45 39 67 Samantha Kennedy 77 31 52 74 83 Robin Bronson 93 94 89 77 97 Sheila Sunny 79 85 28 93 82 Kiran Smith 85 72 49 75 63 @Evinton – mikeg4523 Apr 30 '16 at 07:22
  • When you are working with decimalformat, firstly, verify the computer number format in your laptop or desktop computery. the mistake is here `DecimalFormat("##.##").format((double)sum/(double)a.length);` change '.' to ',' `DecimalFormat("##,##").format((double)sum/(double)a.length);` @mikeg4523 – Evinton Antonio Cordoba Mosque Apr 30 '16 at 22:50
  • after fixing the DecimalFormat("##,##").format((double)sum/(double)a.length) it still gives me the same output – mikeg4523 May 01 '16 at 21:44
  • I think the problem is the read function now that i ve been toying with it the code works until that point. I just don't understand why. – mikeg4523 May 01 '16 at 23:29
  • i don't know where to look other than that.@Evinton Antonio Cordoba Mosque – mikeg4523 May 01 '16 at 23:36