1

hello i have a problem when ever some one open JFileChoose then close it by clicking cancel the IDE give error message the message is below the code i tried to fix it by adding this lines of code

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

but what happen is i get the message from JOptionPane and then the error printed in the 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");}

             }    

     }

the error message

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)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
loverBoy
  • 125
  • 2
  • 13
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson May 01 '15 at 05:52
  • `"there is wrong with the file Note:srt file only"` Rather than show an error message, configure the chooser to only *show* `.srt` files! – Andrew Thompson May 01 '15 at 05:54

1 Answers1

0

you get an error because after you click cancel.. you still executing

this line srtPath = ourSrtFile.getAbsolutePath();

you might want to transfer it here:

 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");
          }

the error printed because it goes to catch:

   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
    }
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40