2012-12-20 34 views
1

当我从JTextField转换为int时,我的GUI程序崩溃。可以帮助我查看这段代码,看看有什么不对,因为我使用了相同的方法,并且工作。从JTextField转换时NumberFormatException

我做什么:

new JTextLabel l; 

得到与什么有

String A = l.getText() >int i = Interger.parse(A); 

代码:

btnAdd.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     int nr = Integer.parseInt(txtApt.getText()); 
     int nrP = Integer.parseInt(txtNrPips.getText()); 
     String owner = txtOwner.toString(); 
     Apartment a = new Apartment(nr, nrP, owner, false); 
     Expense b = new Expense(Float.parseFloat(textWW.getText()), Float.parseFloat(textCW.getText()), 
       Float.parseFloat(textGAS.getText()), Float.parseFloat(txtELEC.getText()), Float.parseFloat(textHEAT.getText())); 
     ArrayList<Expense> l = new ArrayList<>(); 
     l.add(b); 
     cont.addKeyWithList(a,l); 
    } 
}); 

堆栈:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at gui.Gui$4.actionPerformed(Gui.java:195) 
    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$200(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) 
+1

修正是使用try/catch NumberFormatException块。但是什么字符串崩溃呢?例外文本(你应该在这里发布)会告诉你。 –

+0

我添加了堆栈错误 –

+0

@BogdanM。该字符串是''“',换句话说,它是空的,因此不能被解析为数字。 – assylias

回答

5

您的问题是当一个或多个JTextFields为空时会调用此方法,并且当您尝试将“”分析为int时发生错误。如果发生这种情况,解决方法是阻止此方法被调用。例如,您可以使用DocumentListener到您的JTextField文档中,查看JTextFields是否有文本,并保持禁用JButton,直到文本出现在所有必需的文本字段中。

另外,不要使用try/catch块正如我上面捕获异常建议,并妥善地处理它们:

btnAdd.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     try { 
     int nr = Integer.parseInt(txtApt.getText()); 
     int nrP = Integer.parseInt(txtNrPips.getText()); 
     // ... etc ... 
     } catch (NumberFormatException nfe) { 
     // warn the user that the textfields are not parsing well. 
     } 
    } 
}); 
+0

谢谢,现在明白。并且ty也提供关于列表器的暗示 –

+1

@BogdanM:不客气!感谢发布堆栈跟踪,因为按照惯例,它告诉我们大部分问题是什么。 –

2

一些文本字段中没有有效的数字内容,这是窒息解析。为了解决这个问题,你需要预先过滤你的数据。

float floatWW = MY_DEFAULT_VALUE; 
boolean errorWW = false; 
try { 
    float floatWW = Float.parseFloat(textWW.getText()); 
} catch (NumberFormatException ex) { 
    errorWW = true; 
} 

(等了你们每个人parseFloat调用)

接下来,您可以决定是否显示错误或给予不好的普特时,只需接受默认设置。

1

当您将不能转换为数字的字符串转换时,会产生此问题。 例如:有2个字符串变量,一个是“3”,另一个是“三个”。 在以后的情况下,当您尝试转换为整数时,它会给NumberFormatException。

此处存在同样的问题,如果您在文本字段中未提供任何值,则为空白并发生异常。

那么,为了摆脱异常。 首先检查getText()值不应该为空且空白,然后应用正则表达式检查它将只接受不是字母和特殊字符的数字。

相关问题