Java 如果关闭该对话框,将发生NullPointerException。我怎样才能摆脱它?

Java 如果关闭该对话框,将发生NullPointerException。我怎样才能摆脱它?,java,exception,Java,Exception,我目前正在制作一个测验程序,但如果在对话框请求输入时关闭该对话框,则会发生NullPointerException。如何让它执行其他操作,例如显示文本消息和提示输入有效条目 String qn_1_ans = JOptionPane.showInputDialog( null, "What is the answer for Question 1? \nPink \nBlue \nBlack \nRed", JOptionP

我目前正在制作一个测验程序,但如果在对话框请求输入时关闭该对话框,则会发生NullPointerException。如何让它执行其他操作,例如显示文本消息和提示输入有效条目

String qn_1_ans = JOptionPane.showInputDialog(
            null,
            "What is the answer for Question 1? \nPink \nBlue \nBlack \nRed",
            JOptionPane.OK_OPTION
    );

if (qn_1_ans.equals(qn_1_right_ans)) {
        JOptionPane.showMessageDialog(null,
                "Thats correct");
        Score = Score + 1;
    } else {
        JOptionPane.showMessageDialog(null,
                "Your answer is wrong. The correct answer is " +     qn_1_right_ans);
    }

我会先看报纸

返回:
用户的输入,或null表示用户取消了输入


增加了重点

我将从阅读

返回:
用户的输入,或null表示用户取消了输入

重点补充