title: Java:Using an Actionlistener in one class that refrences a variable in another class
body: <p>I have a JTextField and a button in a class called Main.  I have an ActionListener in another class called Action.  I want the ActionListener to be able to refrence the JTextField.  I keep getting a null pointer exception.  I want to keep the JTextField and ActionListener seperate.  I am going to be having many ActionListeners and it would be easier for me to organize it this way.</p>
  
  <pre><code>public class Main  {
  
      public JTextField text;
  
      public JTextField getText(){
          return this.text;
      }
  
  public  static void main(String[] args) {
  
          Main main=new Main();
          main.blah();
      }
  
  public  void blah(){
  
      JFrame myWindow=new JFrame("ff");
      myWindow.setSize(500,500);
      myWindow.setVisible(true);
      myWindow.setDefaultCloseOperation(3);
      text=new JTextField(10);
      JLabel lengthL = new JLabel("Enter a number",SwingConstants.CENTER  );
      JButton button=new JButton("Click button");
      myWindow.getContentPane();
      myWindow.setLayout(new GridLayout(4,4));
      myWindow.add(lengthL);
      myWindow.add(text);
      myWindow.add(button);
  
      Action hand=new Action();
      button.addActionListener(hand);
  }
  }
  
  
  
  public  class Action  implements ActionListener{
  
      @Override
      public void actionPerformed(ActionEvent e) {
          Main main=new Main();
          double length=Double.parseDouble(main.text.getText());
          System.out.println(length);
  
      }
  }
  </code></pre>