filePath from JFileChooser is null
<p>I am currently working on a Project and would like to save an object to a file with <code>ObjectOutputStream</code> to a location the user chooses with the help of a <code>JFileChooser</code>. But the object is always saved to the root directory of the program into the file named "null" (<code>%ProjectDirectory%/null</code>).</p>

<p>Here's my method <code>saveObjects</code>, which saves a <code>LinkedList</code> of objects to a file:</p>

<pre><code>public void saveObjects(String filePath) {
    try {
        FileOutputStream os = new FileOutputStream(filePath);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(oceanObjects);
        oos.close();
        os.close();
    } catch(IOException e) {
        System.err.println(e);
    }
}
</code></pre>

<p>This instruction calls the method <code>saveObject</code> with the <code>filepath</code> as a parameter (<code>filePath</code> is a String; I already tried to use a File)</p>

<pre><code>saveObjects(view.getFilePath());
</code></pre>

<p><code>view</code> is an instance of <code>OceanLifeView</code> and <code>view.getFilePath()</code> is a getter-method of that class that returns the path where the file should be saved (as a String).</p>

<p>getFilePath() looks like this:</p>

<pre><code>public String getFilePath() {
    return filePath;
}
</code></pre>

<p>And my OceanLifeView like this:</p>

<pre><code>OceanLifeView(String title, int type) {
    if(...) {
        ...
    }else if (title.equals("fileChooser")) {
        fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(FILES_ONLY);

        if (type == 0) {
            //Load Button functions
            System.out.println("De-Serialisation started fileChooserGUI!");
            returnVal = fileChooser.showOpenDialog(fileChooser);
        } else {
            //Save Button functions
            System.out.println("Serialisation started fileChooserGUI!");
            returnVal = fileChooser.showSaveDialog(fileChooser);
        }

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            filePath = fileChooser.getSelectedFile();
        }
    }
}
</code></pre>

<p>I would be very thankful to anybody who can share some insight for the problem I encounter or mistake I made implementing this functionality.</p>