我正在創建我的第一個GUI,並且遇到了我似乎無法克服的問題。通過按下按鈕執行一個方法後,我想將JTextArea設置爲「」或null。當按下按鈕時,一個方法執行,程序正常運行,但TextArea不會清空。我得到這個代碼中的「顯示java.lang.NullPointerException」:Java Swing:在方法後設置JTextArea爲空
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);
我可能沒有能提供足夠的信息,以便讓我知道。什麼可能會阻止我的JTextArea清空,我該如何解決它?
其實,'setText'可以帶'null',一直使用它。 – MadProgrammer