我目前正在制作一个测验程序,但如果我在要求输入时关闭对话框,则会发生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);
}