我目前正在制作一个测试程序,但如果我在对话框要求输入时关闭对话框,就会出现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);
}
我将从阅读JavaDocs 开始
返回:
用户的输入,或null表示用户取消了输入
添加了强调