I am trying to make a simple program that can calculate the GPA for any courses that are being currently taken. My issue is that whenever I try to invoke the method by passing the file name of the grades, i get a FILENOTFOUND exception. I checked the path of the file and its in the same directory as my project.
static class Grades {
static void loadGrade(String course, String courseGrade) {
Object[] courseInfo=null;
try (BufferedReader br = new BufferedReader(new FileReader(
courseGrade))) {
String line;
while ((line = br.readLine()) != null) {
courseInfo = line.split("//s+");
if (course == courseInfo[0]) {
break;
}
}
} catch (FileNotFoundException e){
System.out.println("File was not found");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}public static void main(String[] args) {
Grades.loadGrade("Biology", "course_grades");
}
}
this is the error i get:
File was not found
Exception in thread "main" java.lang.NullPointerException
at gpacalculator.GpaCalculator$Grades.loadGrade(GpaCalculator.java:78)
at gpacalculator.GpaCalculator.main(GpaCalculator.java:106)
I ran this in debug mode in Eclipse and it goes straight to the catch block FILENOTFOUND when ran. The null pointer that is thrown is from a for loop that occurs later in the method that cant function because the file cant be found. I just didnt include it for simplicity.
Anyone know how I can tackle this issue? Thanks in advance!
Edit: Forgot to mention explain the format of the text file and the two parameters. The text file is written as
Biology Test 85 Quiz 22
Math Test 90 Quiz 50
So when I read the the lines and put it in an array, the courseInfo[0] refers to the subject. The two parameters are course( biology) and courseGrade( the text file). I inserted a break in the while loop so once the line containing the subject is found, the loop terminates.