public FileWindow(JMenuItem menuItem,final JTextArea text, JMenuItem save){
//Open
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
openFile();
if(fileToOpen == JFileChooser.APPROVE_OPTION){
text.setText("");
try{
Scanner scan = new Scanner(new FileReader(Open.getSelectedFile().getPath()));
while(scan.hasNext()){
text.append(scan.nextLine() + "\n");
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
});
//Save
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
saveFile();
if(fileToSave == JFileChooser.APPROVE_OPTION){
try{
BufferedWriter out = new BufferedWriter(new FileWriter(Save.getSelectedFile().getPath() + ".txt"));
out.write(text.getText());
out.flush();
out.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
});
}
我在这一行收到错误 - > “menuItem.addActionListener(new ActionListener){”我尝试启动变量,但这不起作用。任何帮助将非常感谢!
答案 0 :(得分:0)
您最有可能获得NullPointerException,因为menuitem
为空。确保在调用此构造函数之前初始化menuitem
。
答案 1 :(得分:0)
menuItem
时, menuItem.addActionListener()
为空
确保它不为空,但添加:
if (menuItem == null)
System.out.println("menuItem is null!");