Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:43:06+00:00 2026-06-09T14:43:06+00:00

I got another question that would go to category Java noobs This time my

  • 0

I got another question that would go to category “Java noobs”
This time my problem is following:
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.

My JButton:

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

Now my getChar() method is in different class and looks like this:

public String getChar(){
    inChar = JOptionPane.showInputDialog("Please enter letter (a-z)");
    if (inChar.length() > 1){
        JOptionPane.showMessageDialog(null, "Your Input is incorred, please input char", "Input warning", JOptionPane.WARNING_MESSAGE);
    }
    return inChar;
}

Exception triggers on the line gl.getChar();

And exception is following:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Interface.ButtonPanel$1.actionPerformed(ButtonPanel.java:34)

Any Ideas how to fix it?

EDIT NOTE:

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.

Problem Solved with following ActionListener:

    if (action == "Guess Letter"){
        inputChar = JOptionPane.showInputDialog("Please enter letter (a-z)");
        if (inputChar.length() > 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());
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:43:08+00:00Added an answer on June 9, 2026 at 2:43 pm

    It looks like your main class is probably extending another class since you can call this.actionPerfomed. That means any action on your main class will trigger that method. What you should do instead is add an actionListener to your specific button.

    You need to change the action == comparison to use the String .equals method since you are comparing string values. That == may be triggering the window on startup because both values are null and so it displays the window with a null pointer exception maybe?

    Then here is how you would add an action listener to your specific button the user pushes.

        viewInsertFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(final java.awt.event.ActionEvent evt) {
                displayJOptionPaneHere();
            }
        });
    

    So you only want to display the JOptionPane with an action is performed on that specific button instead of any actionPerformed on the main application.

    I think this is causing the null pointer exception.

     if (inChar.length() > 1){
    

    Before you call that, check if inChar != null. You can wrap your code inside of that, so it only calls the if (inChar.length() > 1 if it’s not null to begin with. This is a common problem I run into all of the time, calling a method on a null object.

    Change it to be like this and your exception should go away.

    public String getChar(){
        inChar = JOptionPane.showInputDialog("Please enter letter (a-z)");
        if (inChar != null) {
           if (inChar.length() > 1){
               JOptionPane.showMessageDialog(null, "Your Input is incorred, please input char", "Input warning", JOptionPane.WARNING_MESSAGE);
           }
         } else {
              inChar = ""
         }
    
        return inChar;
    }
    

    You can protect gl from being null here too then.

    if (gl == null) {
       gl = new gl();
    }
    
        gl.getChar();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got another problem, this time with tables and their borders in HTML (4.01,
I've got the following code from another question on SO to track the changing
Time for another silly question :) I got some tables in a xhtml-document with
I've got another problem in the same code... I'm getting this error: initialization method
This is a newbie question: I have a pre-existing function that I would like
This is borrowed from another question I made on the site, but would have
In another question on stackoverflow I got a hint that I can use thread
This is a very simple question that applies to programming web interfaces with java.
I've got another question regarding to basic Android programming: How can I access the
Yet another PHP question. I've got two arrays: one string-based and the other numeric.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.