Hi there. I'm a newbie and am having what I'm sure is an easily corrected issue. I am using JFileChooser in one class to select a file that I would like to to have passed onto another class for file reading.

private String selectFileOpen(String title) {
        JFileChooser fileopen = new JFileChooser(fileChooserString);
        FileFilter filter = new FileNameExtensionFilter("Text files", "txt");
        int returnVal = fileopen.showDialog(null, title);

        //Valid file loaded
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File f = fileopen.getSelectedFile();
            fileChooserString = f.getPath();
            return f.getPath();
        return "";

In the class I am trying to call the string into i am using the code

FileInputStream fstream = new FileInputStream(i.selectFileOpen.f.getPath());

I am apparently not calling it correctly. Any help?


Member Avatar


What's the error? And what's "i" in "i.selectFileOpen..."?

i is the class that the method selectFileOpen is stored inside of

1) you're returning an empty string. Not null, but a String of length 0: ""
2) you're not reading the String returned by your method.

It looks like you want the String to be the path to the file, and to use that as an argument to your f.getPath(), is that correct?

First thing: build a String and return it from your selectFileOpen method.
Second thing, assign the return value from that method into a String variable, and use that variable as the parameter to f.getPath().

That's the quick answer - details left as an exercise, etc.

And the error is saying that it can't find selectFileOpen

Member Avatar


Yes, you aren't calling it correctly. You have two problems I believe
1. You aren't calling your method
2. Your method doesn't do what you want

To call it correctly, follow your method header: i.selectFileOpen(<title>); It automatically returns a String, so you should probably then do: String s=i.selectFileOpen(<title>); Then the returned String is stored in s.

Yes that is what I am trying to do. Thanks so much. When you say I'm "not reading the String returned" is that because the method was returning an empty string or because there is something wrong with my

      FileInputStream fstream = new FileInputStream(i.selectFileOpen.f.getPath());


Sorry but is title pseudocode? I don't know the title because files are chosen by the user

Member Avatar


Yes, title is pseudocode. You replace it with whatever the String 'name' in your method header is supposed to be.