So I'm building a simple TextEditor with the help of Jpanels But I keep gettin the NullPointerException when I try to assign my JFileChooser on to a variable. I've got 2 classes. A viewer which contains all the Jpanels and a Controller which handles the logic when pressing buttons. I've got a Load button when pressed is supposed to show a file chooser were I can select a text file and append that to my TextArea.
Viewer:
private JFileChooser fc = new JFileChooser();
//....
if(e.getSource() == btnOpen){
controller.loadText();
}
public JFileChooser getFc(){
return fc;
}
Controller:
public void loadText(){
JFileChooser fc = view.getFc(); //Throws NullPointerException
JTextArea taMain = view.getTaMain();
int returnVal = fc.showOpenDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
taMain.append("Opening: " + file.getName() + "." );
} else {
taMain.append("Open command cancelled by user.");
}
}
I do undertstand the exception, but how come this occurs? The TextArea doesn't have to contain anything.