我正在创建我的第一个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