java 取消 showInputDialog 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10657527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:59:22  来源:igfitidea点击:

Error when cancel the showInputDialog

javaswinginputjoptionpane

提问by Chin

I want to create a input dialog box when new record is created and save the user input as inputinto the file.

我想在创建新记录时创建一个输入对话框,并将用户输入另存为input文件。

Here is the code :

这是代码:

String input = JOptionPane.showInputDialog(null, "Enter name :  ", "New Record!", 1);                //This to let user input name
    try
    {
        JOptionPane.showMessageDialog(null, "Thanks for playing.", "Thanks", 1);            //Show this dialog after user input
        HighestScoreFile.HighestScoreFile(input, hours, minutes, seconds, click);                 //Store the data into a file, the `method` is in another class.
    }
    catch(IOException ex){}

When there is no user input or any input, the OKbutton work fine, but if I click the cancelbutton, it returns these errors :

当没有用户输入或任何输入时,OK按钮工作正常,但如果我单击cancel按钮,它会返回以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at HighestScoreFile.HighestScoreFile(HighestScoreFile.java:22)
    at MemoryGame$ButtonListener.actionPerformed(MemoryGame.java:329)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access
String input = ....
if (input!=null) { ...
0(EventQueue.java:101) at java.awt.EventQueue.run(EventQueue.java:666) at java.awt.EventQueue.run(EventQueue.java:664) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue.run(EventQueue.java:680) at java.awt.EventQueue.run(EventQueue.java:678) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:677) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

How can I solve it?

我该如何解决?

回答by Andrew Thompson

JOptionPane.showInputDialog(null, "Enter name :  ", "New Record!", JOptionPane.OK_OPTION);

回答by StepTNT

If the user clicks on "cancel", inputwill be null.

如果用户点击“取消”,input则为空。

You should enter the tryblock only if the user clicks on "ok" or you should add an ifto check if inputis null before trying to use it!

try仅当用户单击“确定”时才应输入该块,或者在尝试使用它之前应添加一个if以检查是否input为空!

Now, you can actually disable the cancel button with

现在,您实际上可以禁用取消按钮

if (input == null){
    // Do something
} else {
    try {
    JOptionPane.showMessageDialog(null, "Thanks for playing.", "Thanks", 1);
    HighestScoreFile.HighestScoreFile(input, hours, minutes, seconds, click);
    } catch(IOException ex){}
}

or checking if the user clicked on cancel with

或检查用户是否点击取消

##代码##