JFileChooser 点击“撤消”按钮,出现空指针异常

学习用JAVA写个记事本,用到了JFileChooser,打开。保存功能都没有问题。但是如果当在对话框时,不选择文件,或者保存路径直接点击“撤消”时,控制台会出现一个空指... 学习用JAVA写个记事本,用到了JFileChooser,打开。保存功能都没有问题。但是如果当在对话框时,不选择文件,或者保存路径 直接点击“撤消”时,控制台会出现一个空指针异常,想问下。怎么解决?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.note.Note.actionPerformed(Note.java:51)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
展开
 我来答
chenyt2009
2010-12-19 · TA获得超过627个赞
知道小有建树答主
回答量:408
采纳率:0%
帮助的人:425万
展开全部
JFileChooser jfc=new JFileChooser("D:\\");
int value=jfc.showSaveDialog(null);//open也一样

if(value==JFileChooser.APPROVE_OPTION){ //判断窗口是否点的是打开或保存

File getPath=jfc.getSelectedFile(); //取得路径,然后该干什么干什么

}else{
// 没有选择,点了取消后要做些什么
}
这样就不会出现异常了~~
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式