JFileChooser错误消息NullPointerException

时间:2015-05-01 05:32:02

标签: java swing nullpointerexception jfilechooser

你好我有一个问题,当有人打开JFileChoose然后关闭它通过单击取消IDE给出错误消息消息是在我尝试通过添加这行代码来修复它的代码

   if(value == JFileChooser.APPROVE_OPTION){
                 ourSrtFile =  ourFileSelector.getSelectedFile();
             }else if(value == JFileChooser.CANCEL_OPTION){
                 JOptionPane.showMessageDialog(null, "you closed with out selecting file");

但发生的事情是我从JOptionPane获取消息,然后在ide

中打印错误
 public void actionPerformed(ActionEvent a){
             Object buttonPressed=a.getSource();
             if(buttonPressed.equals(exitButton))
             {
             System.exit(0);
             }

             if(buttonPressed.equals(openButton)){
                 try{
                 JFileChooser ourFileSelector = new JFileChooser();
                 ourFileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                 int value = ourFileSelector.showOpenDialog(null);
                 if(value == JFileChooser.APPROVE_OPTION){
                     ourSrtFile =  ourFileSelector.getSelectedFile();
                 }else if(value == JFileChooser.CANCEL_OPTION){
                     JOptionPane.showMessageDialog(null, "you closed with out selecting file");
                 }
                 srtPath = ourSrtFile.getAbsolutePath();
                 }catch(HeadlessException exe){
                 exe.fillInStackTrace();
                 }
                try{
                ArrayList<String> ends = ReadingFile.getFileEndingTime(srtPath);
                ArrayList<String> starts = ReadingFile.getFileStartingTime(srtPath);
                ArrayList<String> subs = ReadingFile.readSubtitles(srtPath);
                ArrayList<String> lins = ReadingFile.ArraylineLengths(srtPath);
                for (int i = 0; i < ReadingFile.maxLine(srtPath); i++) {
                 cModel.addRow(new Object[] {lins.get(i), starts.get(i), ends.get(i), subs.get(i)});
                }
             }catch(Exception ex){JOptionPane.showMessageDialog(null, "there is wrong with the file Note:srt file only");}

             }    

     }

错误消息

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at translation.swingInterface$addButtonWatcher.actionPerformed(swingInterface.java:114)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

1 个答案:

答案 0 :(得分:0)

您收到错误,因为单击取消后...仍在执行

这一行srtPath = ourSrtFile.getAbsolutePath();

你可能想在这里转移它:

 if(value == JFileChooser.APPROVE_OPTION){
         ourSrtFile =  ourFileSelector.getSelectedFile();
         srtPath = ourSrtFile.getAbsolutePath(); //<--transfer it here
          }else if(value == JFileChooser.CANCEL_OPTION){
               JOptionPane.showMessageDialog(null, "you closed with out selecting file");
          }

打印错误,因为它会捕获:

   srtPath = ourSrtFile.getAbsolutePath(); //<--cause of the error, ourSrtFile is null because you have clicked cancel
    }catch(HeadlessException exe){
       exe.fillInStackTrace();  // it will print the error in console
    }