0

I am currently making a quiz program but if i close the dialog box when it is asking for an input, a NullPointerException occurs. How do i make it do something else, like display a text message and prompt for a valid entry.

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

1 Answers1

2

I would start by reading the JavaDocs

Returns:
user's input, or null meaning the user canceled the input

Emphasis added

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366