0

I am trying to implement a JDialog at the end of a program and collect the user's input. My problem is that when the user clicks the "Cancel" button, Eclipse shows:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.

The program seems running OK though afterwards. I can still start a new game etc. What is the code that I need to add in order to get rid of this NullPointerException?

msrd0
  • 7,816
  • 9
  • 47
  • 82
user3735871
  • 527
  • 2
  • 14
  • 31
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). 2) Always copy/paste error or exception output. – Andrew Thompson Aug 25 '14 at 09:56
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Aug 25 '14 at 10:00

1 Answers1

1

If you read the docs you'll see that JOptionPane.showInputDialog returns null if the user canceled the input.

You should check your String s for not being before using it.

msrd0
  • 7,816
  • 9
  • 47
  • 82
John Doe
  • 146
  • 4
  • Thanks.Yes I did check the string s. But when the user clicks Cancel it still returns an nullPointerException. I'd like to know how to handle this and close the InputDialog (but still keeps the program running). – user3735871 Aug 25 '14 at 10:39
  • Then you should provide more code. From what I see I can't tell more. – John Doe Aug 25 '14 at 10:41