1

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.

June
  • 23
  • 4
  • You are passing `course_grades`, as the file name. This value doesn't have any folder path and file extension. Make sure that value that you are passing contains correct path and complete file name. – Armaiti Apr 17 '17 at 17:25
  • `if (course == courseInfo[0])` is not the correct way to compare Strings in Java. See http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java . Also, I suspect your regular expression is wrong; you need to be aware of the difference between forward slashes and backslashes. – VGR Apr 17 '17 at 17:30
  • @VGR thanks for the tip! I never understood the difference between the two but now I understand a lot better. Thanks! – June Apr 17 '17 at 19:47
  • @Armaiti So how can i add file path to it? I thought Eclipse would search my directory using the file name. – June Apr 17 '17 at 19:49
  • @June A running process is not aware of where the files are, you should specify that in your program. Eclipse doesn't search your directories. – Armaiti Apr 17 '17 at 19:54

1 Answers1

1

When you are building app with Maven or Gradle the output JAR file goes usually in target/ directory in your project dir. And when you provide relative path to file it may be relative to the JAR file being executed (depending on your IDE / launch config).

Option 1: pass full file path.

Option 2: pass path relative to build output directory, for example (but only for testing!) ../course_grades.

Radzikowski
  • 2,377
  • 4
  • 27
  • 39