user2941526
user2941526

Reputation: 497

Java Swing: Setting a JTextArea to Null After Method

I am creating my first GUI and I've run into a problem I can't seem to overcome. After executing a method by a button press, I want to set a JTextArea to "" or null. At the minute when the button is pressed a method executes and the program runs as normal but the TextArea does not empty. I get a "java.lang.NullPointerException" within this code:

    JButton JInputFile = new JButton("Input network file");
    JInputFile.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            final JFileChooser fc = new JFileChooser();
            int returnVal = fc.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                String fileName = file.getName();
                network = new Network(fileName);
                JTextAreaResult.append(null);  //exception points to this line
            }
        }
    });
    JInputFile.setFont(new Font("Calibri", Font.PLAIN, 11));
    JInputFile.setBounds(20, 56, 294, 20);
    contentPane.add(JInputFile);

I may not have provided enough information, so let me know. What may be preventing my JTextArea from emptying, and how can I fix it?

Upvotes: 1

Views: 1160

Answers (3)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

  • For one, it's not .append(null); but rather .setText("");.
  • For another, is your JTextAreaResult variable null in the scope of where you're trying to use it?
  • And for third, your variable names should conform to Java naming standards and should begin with a lower-case letter. This helps others (us!) better read and understand your code.
  • Also, you appear to be using a null layout and calling setBounds(...) on your components. While this may seem to a newbie the better way to create complex GUI's, it's a fallacy, and more you create Swing GUI's the more you learn to respect and use the layout managers and see that these creatures help immensely in creating flexible, beautiful and if need be, complex GUI's.

Upvotes: 4

Reinstate Monica
Reinstate Monica

Reputation: 2798

"I want to set a JTextArea to "" or null."

These things are not the same thing. "" is a reference to an empty String object. null on the other hand, is a reference to nothing. What it means to get a NullPointerException, is that your program tried to follow some reference, but it was a null reference.

If you want empty your textfield, set the text to an empty string using "". Do NOT set anything to null.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347204

Two things come to mind...

Rather than using append, you should use setText. Append does exactly what it sounds like, it appends the text to the end of the underlying Document of the JTextArea, where as setText will replace the contents of the Document with the new value.

And/or JTextAreaResult is null

Upvotes: 2

Related Questions