title: Assign text from JTextField to File after user provides string
body: <p>My program is designed to be able to read a file, search within a file, and write to a file (each with a corresponding JButton) - depending on user input. It was initially a console app, but now I'm working on a GUI for it (using SWING). The issue I'm having is that the file (string format) the user specifies (types in JTextField) is read into the "targetFile" variable before the user provides input - resulting in a null value.</p>
  
  <p>I want the program to wait until after the user clicks the desired button to read the string into the "targetFile" variable.</p>
  
  <p>Here's the minimum code for what I'm trying to accomplish:</p>
  
  <pre><code>public class SimpleDBGUI {
  static File targetFile;     //Declare File var to be used in methods below for holding user's desired file
  
  public void mainWindow(){
  
      //Create main window for Program
      JFrame mainWindow = new JFrame("Simple Data Base");     //Init frame
      mainWindow.setSize(500, 180);       //Set frame size
      mainWindow.setVisible(true);        //Make frame visible
  
      //Create panel for the main window of the GUI
      JPanel simpleGUI = new JPanel( new GridBagLayout());
      GridBagConstraints gbCons = new GridBagConstraints();
      mainWindow.getContentPane().add(simpleGUI);         //Adds JPanel container to the ContentPane of the JFrame
  
      //Create button linking to read function
      JButton readButton = new JButton("Read");       //Init button, and give text
      gbCons.fill = GridBagConstraints.BOTH;
      gbCons.gridx = 0;
      gbCons.gridy = 2;
      gbCons.weightx = .1;
      readButton.setActionCommand("Read");
      readButton.addActionListener( new ButtonClickListener());
      simpleGUI.add(readButton, gbCons);      //Adds the "Read" button to the JPanel
  
      //Create TextField for user to input a desired file
      JTextField sdbTarget = new JTextField();
      gbCons.fill = GridBagConstraints.BOTH;
      gbCons.gridx = 0;
      gbCons.gridy = 1;
      gbCons.gridwidth = 3;
      simpleGUI.add(sdbTarget, gbCons);               //Adds TextField to GUI
      targetFile = new File(sdbTarget.getText());     //Writes input string to a File var, works but out of time resulting in null value
  }
  
      public class ButtonClickListener implements ActionListener{         //Sets the EventListener for every function
  
          public void actionPerformed(ActionEvent event){
  
              String function = event.getActionCommand();                                 //Reads the ActionCommand into a string for use in performing desired function
              if( function.equals("Read")){                                               //Read function
                  Desktop desktop = Desktop.getDesktop();                                 //For the GUI version, supposed to open "targetFile" upon readButtonClick
                  try {
                      desktop.open(targetFile);                                           //Surrounded with Try/Catch because Java complains
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }               
              }
          }
      }
  </code></pre>
  
  <p>I made a reference to this class in main since you can't use directly use main to create a GUI. The null value returned by the line "targetFile = new File()" causes an IllegalArgumentException, saying that my file does not exist. </p>
  
  <p>I ran the code through a Debugger, stepping the whole way, and it worked flawlessly. When I ran it normally the aforementioned line doesn't give the user time to input a file path, or wait for the user to click the button, before trying to grab text (that doesn't yet exist) form the JTextField.</p>
  
  <p>This is my initial stack-trace:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file:  doesn't exist.
  at java.awt.Desktop.checkFileValidation(Desktop.java:210)
  at java.awt.Desktop.open(Desktop.java:270)
  at simpleDatabase.SimpleDBGUI$ButtonClickListener.actionPerformed(SimpleDBGUI.java:108)
  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
  at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
  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:6535)
  at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
  at java.awt.Component.processEvent(Component.java:6300)
  at java.awt.Container.processEvent(Container.java:2236)
  at java.awt.Component.dispatchEventImpl(Component.java:4891)
  at java.awt.Container.dispatchEventImpl(Container.java:2294)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
  at java.awt.Container.dispatchEventImpl(Container.java:2280)
  at java.awt.Window.dispatchEventImpl(Window.java:2750)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
  at java.awt.EventQueue.access$500(EventQueue.java:97)
  at java.awt.EventQueue$3.run(EventQueue.java:709)
  at java.awt.EventQueue$3.run(EventQueue.java:703)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
  at java.awt.EventQueue$4.run(EventQueue.java:731)
  at java.awt.EventQueue$4.run(EventQueue.java:729)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
  </code></pre>
  
  <hr>
  
  <pre><code>public class ButtonClickListener implements ActionListener{         //Sets the EventListener for every function
  
      public void actionPerformed(ActionEvent event){
  
          File targetFile = new File(sdbTarget.getText());String function = event.getActionCommand();                                 //Reads the ActionCommand into a string for use in performing desired function
              if( function.equals("Read")){                                               //Read function
                  Desktop desktop = Desktop.getDesktop();                                 //For the GUI version, supposed to open "targetFile" upon readButtonClick
                  desktop.open(targetFile);               
  </code></pre>
  
  <p>My current stack-trace:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at simpleDatabase.SimpleDBGUI$ButtonClickListener.actionPerformed(SimpleDBGUI.java:101)
  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
  at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
  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:6535)
  at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
  at java.awt.Component.processEvent(Component.java:6300)
  at java.awt.Container.processEvent(Container.java:2236)
  at java.awt.Component.dispatchEventImpl(Component.java:4891)
  at java.awt.Container.dispatchEventImpl(Container.java:2294)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
  at java.awt.Container.dispatchEventImpl(Container.java:2280)
  at java.awt.Window.dispatchEventImpl(Window.java:2750)
  at java.awt.Component.dispatchEvent(Component.java:4713)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
  at java.awt.EventQueue.access$500(EventQueue.java:97)
  at java.awt.EventQueue$3.run(EventQueue.java:709)
  at java.awt.EventQueue$3.run(EventQueue.java:703)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
  at java.awt.EventQueue$4.run(EventQueue.java:731)
  at java.awt.EventQueue$4.run(EventQueue.java:729)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
  </code></pre>