JFileChooser 错误消息 NullPointerException

JFileChooser error message NullPointerException

你好我有一个问题,当有人打开 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.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:694)
at java.awt.EventQueue.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue.run(EventQueue.java:708)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)

你得到一个错误,因为在你点击取消之后..你仍然在执行

这一行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
    }