title: Java: How to open JOptionPane with JButton
body: <p>I got another question that would go to category "Java noobs" <br>
  This time my problem is following: <br>
  I need person to be able to click on button and it would display JOptionPane. However once I click on button it gives me exception.</p>
  
  <p>My JButton:</p>
  
  <pre><code>    inputChar = new JButton("Guess Letter");
      add(inputChar);
      this.add(Box.createVerticalStrut(10));
      inputChar.setAlignmentX(Component.CENTER_ALIGNMENT);
      inputChar.addActionListener(this);
  
  
  
  public void actionPerformed(ActionEvent e) {
  
      String action = e.getActionCommand();
  
      if (action == "Guess Letter"){
          gl.getChar();
      }   
  </code></pre>
  
  <p>Now my getChar() method is in different class and looks like this:</p>
  
  <pre><code>public String getChar(){
      inChar = JOptionPane.showInputDialog("Please enter letter (a-z)");
      if (inChar.length() &gt; 1){
          JOptionPane.showMessageDialog(null, "Your Input is incorred, please input char", "Input warning", JOptionPane.WARNING_MESSAGE);
      }
      return inChar;
  }
  </code></pre>
  
  <p>Exception triggers on the line <code>gl.getChar();</code></p>
  
  <p>And exception is following:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at Interface.ButtonPanel$1.actionPerformed(ButtonPanel.java:34)
  </code></pre>
  
  <p>Any Ideas how to fix it? </p>
  
  <p><strong>EDIT NOTE:</strong></p>
  
  <p>I managed to fix it with Loagans tip.
  Basically in GuessedLetters class I just set constructors setters/getters and in action listener I put in method which sets them. </p>
  
  <p>Problem Solved with following ActionListener:</p>
  
  <pre><code>    if (action == "Guess Letter"){
          inputChar = JOptionPane.showInputDialog("Please enter letter (a-z)");
          if (inputChar.length() &gt; 1){
              JOptionPane.showMessageDialog(null, "Your Input is incorred, please input char", "Input warning", JOptionPane.WARNING_MESSAGE);
          }else{
          GuessedLetters glr = new GuessedLetters(inputChar);
          glr.setInChar(inputChar);
          //For testing purposes
          System.out.println(glr.getInChar());
          }
  </code></pre>