Reputation: 189
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 ?
Upvotes: 0
Views: 273
Reputation: 4453
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
.
Upvotes: 1
Reputation: 2399
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;
}
}
Upvotes: 1
Reputation: 451
I think this is problem:
if(result == JFileChooser.APPROVE_OPTION)
subFile = this.getSelectedFile();
result == JFileChooser.APPROVE_OPTION
return false and subFile
never initialized
Upvotes: 0
Reputation: 950
Well you are never calling createAndShowGUI()
. Just call that before you call getFile()
Upvotes: 2