从JTextField转换时的NumberFormatException

Bog*_* M. 1 java swing typeconverter jtextfield

当我转换JTextField为int 时,我的GUI程序崩溃了.可以帮助我查看这段代码,看看有什么问题,因为我使用了相同的方法,并且它有效.

我所做的:

new JTextLabel l;
Run Code Online (Sandbox Code Playgroud)

在那里得到什么

String A = l.getText() >int i = Interger.parse(A);
Run Code Online (Sandbox Code Playgroud)

码:

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

堆:

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)
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 5

您的问题是当一个或多个JTextField为空时调用此方法,并且当您尝试将""解析为int时发生错误.解决方案是在发生这种情况时阻止调用此方法.例如,您可以将DocumentListener用于JTextField文档,以查看JTextField是否具有文本,并在所有必要的文本字段中都存在文本之前禁用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.
      }
    }
});
Run Code Online (Sandbox Code Playgroud)