0

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.

Essej
  • 892
  • 6
  • 18
  • 33
  • We can't *possibly* answer this without knowing where view comes from, which makes me think you don't understand the exception. – djechlin Dec 28 '15 at 15:16
  • 1
    Look at this: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – Matt Dec 28 '15 at 15:17
  • view is just an object of Viewer. – Essej Dec 28 '15 at 15:18
  • 1
    It looks like either `view` is null, or NPE is thrown inside `getFc()`. We can't help you much without [SSCCE](http://sscce.org) - minimal but full example which will let us reproduce your problem. – Pshemo Dec 28 '15 at 15:18

0 Answers0