0

I'm getting a NullPointerException every time I call a getter. Here's the code:

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileChooser extends JFileChooser {
private int result;
private File filename;
private File subFile;
private FileNameExtensionFilter filter;

    public FileChooser() {
        filename = new File(System.getProperty("user.home"));
        filter = new FileNameExtensionFilter("Subtitle Files (*.srt)", "srt");
    }

    public void createAndShowGUI() {
        this.setDialogTitle("Select a file");
        this.setCurrentDirectory(filename);       
        this.setFileFilter(filter);
        result = this.showOpenDialog(this);
        if(result == JFileChooser.APPROVE_OPTION) 
            subFile = this.getSelectedFile();   
        this.setVisible(true);        
    }

    public File getFile() {
        return subFile;
    }

}

I get null pointer everywhere I use the variable that calls the getFile() function. foo in this case. Here's the snippet:

FileChooser fileChooser = new FileChooser();
File foo = fileChooser.getFile();
System.out.println(foo.getName()); //Null-Pointer
System.out.println(foo.getPath()); //Null-Pointer  

UPDATE: I happen to have two instances of FileChooser but, I would like to call createAndShowGUI() only once. How do I initialise the selected file in the constructor ?

Jimmy
  • 189
  • 9

4 Answers4

2

Well you are never calling createAndShowGUI(). Just call that before you call getFile()

TameHog
  • 950
  • 11
  • 23
1

According to your code, the field subFile is initialized in the method createAndShowGUI() inside the if condition.

But, as you are not calling the method createAndShowGUI() the field subFile remains null when you call getFile().

Try this code,

FileChooser fileChooser = new FileChooser();
fileChooser.createAndShowGUI();             // MISSING IN ORIGINAL CODE
File foo = fileChooser.getFile();
System.out.println(foo.getName()); //Null-Pointer
System.out.println(foo.getPath()); //Null-Pointer

UPDATE

As the question is updated with

I happen to have two instances of FileChooser but, I would like to call createAndShowGUI() only once

If you call createAndShowGUI() only once then the user will be able to select the file only once.

Then what is the use of creating two instances of FileChooser?

You need to call createAndShowGUI() every time you create a new instance of FileChooser.

Shrinivas Shukla
  • 4,325
  • 2
  • 21
  • 33
1
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileChooser extends JFileChooser {
    private int result;
    private File filename;
    private File subFile;
    private FileNameExtensionFilter filter;

    public FileChooser() {
        filename = new File(System.getProperty("user.home"));
        filter = new FileNameExtensionFilter("Subtitle Files (*.srt)", "srt");
        this.setDialogTitle("Select a file");
        this.setCurrentDirectory(filename);       
        this.setFileFilter(filter);
        result = this.showOpenDialog(this);
        if(result == JFileChooser.APPROVE_OPTION) 
            subFile = this.getSelectedFile();   
        this.setVisible(true);
    }

    public File getFile() {
        return subFile;
    }

}
aleksandar
  • 2,399
  • 2
  • 14
  • 22
0

I think this is problem:

if(result == JFileChooser.APPROVE_OPTION) 
        subFile = this.getSelectedFile();  

result == JFileChooser.APPROVE_OPTION return false and subFile never initialized

Bohdan Zv
  • 442
  • 2
  • 5
  • 22