我写过一个程序&我为它构建了一个GUI。当我尝试运行它时,它会引发异常NumberFormatException
,这就是我得到的全部内容:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at newtp.GUI$2.actionPerformed(GUI.java:219)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
那么我该怎么办才能解决我的问题?
答案 0 :(得分:0)
你的问题是你的字符串是空的
java.lang.NumberFormatException: empty String at
要解决此问题,您应该使用数字的字符串表示填充字符串(例如,字符串为3,双字符为7.7)
如果您向我们展示您的代码以查看问题所在,那将是最好的。
示例:
如果您执行以下操作
String string1 = "";
int int1 = Integer.parseInt(string1);
您将收到您收到的异常,因为Java无法为空字符串找到匹配的int值。
如果你这样做
String string2 = "3";
int int2 = Integer.parseInt(string2);
你不会收到异常,但是一个名为int2的变量包含值3。
答案 1 :(得分:0)
正如Phiwa所说,一个解决方案就是不要传入一个空字符串。但是,处理这个问题可能更好。
如果输入的值不是正确的类型,您可以使用类似的内容来指定默认值:
int x;
try {
//whatever you're doing to parse the string...probably x = Integer.parseInt("32"); or something
catch(NumberFormatException e) {
e.printStackTrace();
x = 12; //or whatever default value you want
}
答案 2 :(得分:0)
当它变为空时,我已经将变量输出,这是在actionListener中,所以我带来了值(使用textField.getText();)解析完成了...我忘记引入所有的方法.. 感谢帮助家伙:)))