所以我遇到了这个问题,一个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));
终端sais 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
的确切含义。它是什么意思,我怎么能让它在未来不发生?
答案 0 :(得分:3)
似乎你试图解析一个空字符串,因为永远不会调用tPages = "0";
。
在if语句中使用tPages.isEmpty()
而不是tPages == ""
答案 1 :(得分:0)
在评估对象值时,您需要使用对象中的运算符而不是基本格式。
对于String
和其他复杂对象,您需要使用内部方法来比较它们的内部值。您需要使用tPages.equals("")
,tPages.isEmpty()
以及您可以看到here而不是==""的其他值,您可以查看有关此{{1}的详细信息3}}。
对于您的NumberFormatException
,它是Number对象的常见例外,当您创建格式错误的数字时,一次""不是数字。
答案 2 :(得分:-2)
尝试0.0而不是0,0。看看它是否有效。