-1

I wanted to run a Java program I wrote two years ago. It needs two files as command line parameters and reads them. This program has worked great before and as it was a school project it went through many tests to make sure everything was working correctly. I downloaded the project from my submission to make sure I had the same version. Only thing this version lacked was the files to read because we were asked not to include them and rather use a path so they don't accidentally get tracked by version control. I added the files to the same directory as the main java file. When I run the program I get:

Welcome to "Name of school project"
Exception in thread "main" java.lang.NullPointerException
    at practice.collection.Collection.download(Collection.java:100)
    at practice.UI.UI.mainLoop(UI.java:63)
    at mainFile.main(mainFile.java:60)

This is what download method in Collection looks like:

    public void download(String fileName) {
        Scanner fileReader = null;
        try {
            File file = new File(fileName);
            fileReader = new Scanner(file);
            while (fileReader.hasNextLine()) {
               ***reads lines and does some sorting***
            }
            fileReader.close();
        }
        catch (FileNotFoundException | NumberFormatException e) {
            fileReader.close(); ***this is line 100***
            System.out.println("Missing file!");
            System.out.println("Program terminated.");
            System.exit(0);
        }
    }

I have also made sure the files to be downloaded are the same as before, they are not empty and are being called with correct spelling, in correct order. Like this: java mainFile first_file.txt second_file.txt. Why is the program not finding the files like before?

I did check out What is a NullPointerException, and how do I fix it? but does not answer my question. I assume I get the exception because my program can't find the file and is thus referring to a file object with null value. I am trying to figure out why the file can't be found and read. I think I should be able to fix this problem without touching the code. The program behaves the same way regardless of if the files have been included.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
Samkayoki
  • 59
  • 5
  • 2
    The nullpointer exception is because you never got a chance to initialize fileReader before you got your *INITIAL* exception. 1) You should move `fileReader.close()` into a separate `finally {}` clause. 2) The "root cause" is that you're getting a "FileNotFoundException" or a "NumberFormatException". That's the part you need to troubleshoot! – paulsm4 Jan 01 '23 at 17:42
  • Like I said above: 1) Remove "fileReader.close()" from your "catch{}" and add it to a new, separate "finally{}", e,g, `finally { if (fileReader != null) fileReader.close(); }` 2) Troubleshoot whether the file you *THINK* is there is *ACTUALLY* present, and where you think it should be. `System.out.println(System.getProperty("user.dir"))` can help. – paulsm4 Jan 01 '23 at 17:52

1 Answers1

1

Suggested changes for troubleshooting the underlying problem:

public void download(String fileName) {
    System.out.println("fileName=" + fileName + "...");
    System.out.println("current directory=" + System.getProperty("user.dir"));
    Scanner fileReader = null;
    try {
        File file = new File(fileName);
        fileReader = new Scanner(file);
        while (fileReader.hasNextLine()) {
           ***reads lines and does some sorting***
        }
        fileReader.close();
    }
    catch (FileNotFoundException | NumberFormatException e) {
        System.out.println(e);  // Better to print the entire exception
        //System.out.println("Missing file!"); // Q: What about NumberFormatException?
        System.out.println("Program terminated.");
        System.exit(0);
    }
    finally {
       // This assumes your app won't be using the scanner again
       if (fileReader != null)
           fileReader.close(); 
    }
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Something is going wrong with running the app, while adding this I realized I will get the same exception no matter what i do to the code. Thought I opened the wrong folder with vscode or navigated wrong with terminal but no. Obviously saved changes and ran javac before also. Error messages had to be those ones to pass the school testing. – Samkayoki Jan 01 '23 at 18:54
  • Q: Did you try the code above? Q: Do you still get a NullReferenceException on "fileReader.close()" (I'm sure "No"). Q: What was the exception (probably "FileNotFoundException" - BUT WE NEED TO CONFIRM THIS! Q: What is the println for "current directory"? For "FileName"? PLEASE ADD THIS TO YOUR QUESTION! Q: Does the file exist? In the expected directory? – paulsm4 Jan 01 '23 at 19:08
  • Yes, I copied your changes exactly. I never got a NullReferenceException, but I am still getting a NullPointerException on line 100. Nothing is getting printed. By "no matter what I do to the code" I mean I can randomly delete half of the lines and still get the same exception. The changes are not being registered even though I checked that I'm editing and running the correct file, saving and running javac before. – Samkayoki Jan 01 '23 at 19:16
  • In other words, the problem seems to be with your project's configuration. SUGGESTION: 1) Create a new, minimal test project to verify that you can compile *ANY* (e.g. "hello world") app with your IDE (e.g. VSCode), then 2) Create a new, clean project for your old test code, 3) Verify "everything works". – paulsm4 Jan 02 '23 at 19:36