本帖最后由 薆情媬証書 于 2013-10-27 17:24 编辑
- <p>openMi.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- FileDialog opDia = new FileDialog(f, "打开…………");
- opDia.setVisible(true);
- String filePath = opDia.getDirectory();
- String fileName = opDia.getFile();
- file = new File(filePath, fileName);
- if (filePath==null || fileName==null)
- return;</p><p> ta.setText("");
- try
- {
- BufferedReader br = new BufferedReader(new FileReader(file));
- String line = null;</p><p> while ((line = br.readLine() )!=null)
- {
- ta.append(line+"\r\n");
- }
- br.close();
- }
- catch (IOException s)
- {
- throw new RuntimeException("文件打开失败");
- }</p><p> }</p><p>}
- </p>
复制代码 这段代码哪里错了,当我点菜单栏中的打开时,然后不选择任何文件,点击取消,控制台会出现NullPointException,关于文件路径和文件名,我已经判断过了啊,怎么回事啊?
错误提示如下:- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
- at java.io.File.<init>(File.java:262)
- at MyGui$2.actionPerformed(MyText.java:69)
- at java.awt.MenuItem.processActionEvent(MenuItem.java:627)
- at java.awt.MenuItem.processEvent(MenuItem.java:586)
- at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:300)
- at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:288)
- at java.awt.EventQueue.dispatchEvent(EventQueue.java:602)
- at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
- ad.java:273)
- at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
- java:183)
- at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
- ad.java:173)
- at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
- at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
- at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
复制代码 |