简体   繁体   中英

Java: How to open JOptionPane with JButton

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM