title: Opening up FileChooser in Eclipse Plugin Development
body: <p>I am creating an Eclipse-Plugin where I need to open the file system browser, choose a file and then perform certain computation on the chosen file.</p>
  
  <p>I have tried using <code>JFileChooser</code> (where I have a class which opens up the file browser). This particular class is called from my handler class and once the file is chosen (from the file chooser), I need to return the String (of the files location) and perform computation on it. However, I am experiencing difficulties with this because I am unable to perform this computation after closing the <code>JFrame</code>.</p>
  
  <pre><code>public class FileChooserDialog {
  
         private JFrame mainFrame;
         private JLabel headerLabel;
         private JLabel statusLabel;
         private JPanel controlPanel;
         private String selectedFile = null;
  
         public FileChooserDialog(){
            prepareGUI();
            clickExtractFile();
         }
  
         public String getSelectedFile(){
             return selectedFile;
         }
  
         private void prepareGUI(){
            mainFrame = new JFrame("choose a file");
            mainFrame.setSize(400,400);
            mainFrame.setLayout(new GridLayout(3, 1));
  
            headerLabel = new JLabel("", JLabel.CENTER);   
            headerLabel.setText("Choose a valid zip file to extract");
            statusLabel = new JLabel("",JLabel.CENTER);    
  
            statusLabel.setSize(350,100);
            statusLabel.setText("No file selected");
  
            controlPanel = new JPanel();
            controlPanel.setLayout(new FlowLayout());
  
            mainFrame.add(headerLabel);
            mainFrame.add(controlPanel);
            mainFrame.add(statusLabel);
            mainFrame.setVisible(true); 
         }
  
         private void clickExtractFile(){
            final JFileChooser  fileDialog = new JFileChooser();
            JButton showFileDialogButton = new JButton("Open File");
            showFileDialogButton.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent e) {
                  int returnVal = fileDialog.showOpenDialog(mainFrame);
                  if (returnVal == JFileChooser.APPROVE_OPTION) {
                     File file = fileDialog.getSelectedFile();
                     statusLabel.setText(file.getAbsolutePath());
                  }
                  else{
                     statusLabel.setText("No file selected");           
                  }      
               }
            });
            JButton extractButton = new JButton("Choose This");
  
            extractButton.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent e) {
                   selectedFile = statusLabel.getText();
  
                   if (selectedFile.equals("No file selected")){
                       selectedFile = "No file";
                       System.out.println("nahh nothing selected"); 
                   }else{
                       System.out.println("yes a file was selected. This was: " + selectedFile); 
                   }
                   mainFrame.dispatchEvent(new WindowEvent(mainFrame, WindowEvent.WINDOW_CLOSING));
               }
            });
  
            controlPanel.add(showFileDialogButton);
            controlPanel.add(extractButton);
            mainFrame.setVisible(true); 
         }
  </code></pre>
  
  <p>Handler class:</p>
  
  <pre><code>public class Handler extends AbstractHandler{
          FileChooserDialog chooser = new FileChooserDialog();
  
          chooser.clickExtractFile();
  
          //I only want this to be called once the Jframe is closed (i.e. after user clicks "Choose This"
          fileChosen = chooseZip.getSelectedFile();
  }
  </code></pre>
  
  <p>However I receive a nullpointer exception when <code>fileChosen = chooseZip.getSelectedFile();</code> is executed as it is getting executed even before the JFrame is closed.</p>
  
  <p>Exception:</p>
  
  <pre><code>org.eclipse.e4.core.di.InjectionException: java.lang.NullPointerException
      at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:62)
      at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:247)
      at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:229)
      at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
      at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:149)
      at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
      at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
      at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
      at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:825)
      at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.handleWidgetSelection(HandledContributionItem.java:701)
      at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access$6(HandledContributionItem.java:685)
      at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem$4.handleEvent(HandledContributionItem.java:613)
      at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
  </code></pre>
  
  <p>How can I solve this?</p>