[英]Exception when clicks Cancel button in JDialog
I am trying to implement a JDialog
at the end of a program and collect the user's input. 我正在尝试在程序末尾实现
JDialog
并收集用户的输入。 My problem is that when the user clicks the "Cancel" button, Eclipse shows: 我的问题是,当用户单击“取消”按钮时,Eclipse显示:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.
线程“ 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
? 我仍然可以开始新游戏等。为了摆脱此
NullPointerException
,需要添加什么代码?
If you read the docs you'll see that JOptionPane.showInputDialog
returns null
if the user canceled the input. 如果您阅读文档,将会看到如果用户取消了输入,则
JOptionPane.showInputDialog
返回null
。
You should check your String s
for not being before using it. 在使用它之前,应检查
String s
是否不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.