This question already has answers here:
What is the proper way to handle a NumberFormatException when it is expected?

(8个答案)


4年前关闭。




所以我遇到了一个GUI女巫的问题,巫婆必须从五个JTextField中获取一些信息,并将该信息放入类libro(本书的意大利语)的实例中。

我已经进行了一些控制,如果要检查接收到的字符串是否不为空,但是我在终端中始终出现NumberFormatExeption错误。

NumberFormatExeption发生部分的代码

tTitle  = this.titleField.getText();
if(tTitle == null || tTitle == "")
    tTitle = "Titolo Non Conosciuto";
tAuthor  = this.authorField.getText();
if(tAuthor == null || tAuthor == "")
    tAuthor = "Autore Non Conosciuto";
tEditor = this.editorField.getText();
if(tEditor == null || tEditor == "")
    tEditor = "Editore Non Conosciuto";
tPages  = this.pagesField.getText();
if(tPages == null || tPages == "")
    tPages = "0";
tPrezzo  = this.priceField.getText();
if(tPrezzo == null || tPrezzo == "")
    tPrezzo = "0,0";
temp = new Libro(tTitle,tAuthor,tEditor,Integer.parseInt(tPages),Double.parseDouble(tPrezzo));


终端说NumberFormatExeption发生在上面代码的最后一条语句中。如果需要,这里是完整的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at MyPanel.actionPerformed(MyPanel.java:148)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    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:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


我什至不知道NumberFormatExeption是什么意思。这是什么意思,将来如何使其不发生?

最佳答案

似乎您尝试解析空字符串,因为从未调用tPages = "0";

在if语句中使用tPages.isEmpty()而不是tPages == ""

07-26 03:27