Still get a NullPointerException

If the user clicks on the cancel button in a JOptionPane box I get a NullPointerException. Even when I added the code for the Cancel button I still get a NullPointerException.

public String askQuestion(String question)

{

String answerToQuestion = JOptionPane.showInputDialog(question);

while (answerToQuestion.equals("") || Integer.parseInt(answerToQuestion)

==JOptionPane.CANCEL_OPTION))

{

answerToQuestion = JOptionPane.showInputDialog(question);

}

return answerToQuestion;

}

What's the problem?

Thanks

[820 byte] By [java4life87a] at [2007-10-3 3:30:34]
# 1

The only things that I can see that could be null here are question and answerToQuestion.

I haven't read the docs for showInputDialog, so I don't know if it returns null or under what circumstances. You can read up on that yourself.

I don't know how you're calling this method, so I can't tell if question is null. You could print question as the first step of this method to find that out.

jverda at 2007-7-14 21:24:28 > top of Java-index,Java Essentials,Java Programming...
# 2
It does return null if the user didn't select anything.Why not set more options in the dialog to restrict the choices so you don't have to reshow the dialog if you don't like the answer?
ejpa at 2007-7-14 21:24:28 > top of Java-index,Java Essentials,Java Programming...
# 3
I actually used a while loop and a try catch statement and it worked. Thanks
java4life87a at 2007-7-14 21:24:28 > top of Java-index,Java Essentials,Java Programming...