NumberFormatException的问题

时间:2014-09-17 11:31:17

标签: java string parsing jlabel numberformatexception

我注意到我的程序存在一个奇怪的问题,我无法理解。我已经包含了已被标记的方法,以及给出的错误。让我感到困惑的是,我在其他按钮上重复了这种方法大约40次,只改变了' engtf4'到需要的另一个来源。它们完美无缺,但编写完全相同。进一步混淆,该方法完全按预期执行(即使给出了下面的错误消息)?

我使用关键字查找网络中的类似问题 - java.lang.NumberFormatException:对于输入字符串:"",但我注意到我看到的所有示例网站都引用了特定值在qoute标记。例如, - java.lang.NumberFormatException:对于输入字符串:" 2345"。我将不胜感激,谢谢。

方法来源代码:

if (a.getSource() == engBuy4){
    getItems();
q = engtf4.getText();
qq = Long.parseLong(q);  //////////// LINE 13086

if (qq > 1000000 || total > 1000000){
    Error.setText("You can ship a maximum of 1 million items.");
    engtf4.setText("");
}
if (qq <= 1000000 && total <= 1000000){
    if (qq > rem){
    Error.setText("You can ship " + rem + " more items");
    engtf4.setText("");             
}
if (qq <= rem){
    buyShrEng();
    engtf4.setText("");             
}
}

错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at DopeWars.DopeWars.mouseReleased(DopeWars.java:13086)
    at java.awt.AWTEventMulticaster.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)

4 个答案:

答案 0 :(得分:2)

如果传递的字符串不是可解析的long值,则

Long.parseLong()会抛出NumberFormatException。在您的情况下,您传递一个空字符串(可能是因为用户没有在输入字段中输入任何内容)。您可以轻松验证:

...
Long.parseLong("");
...
java.lang.NumberFormatException: For input string: ""

您应该始终准备好捕获此异常(除了空字符串之外,如果用户输入任何不是Long,则抛出异常),并显示相应的错误消息给用户,比如

try {
    qq = Long.parseLong(q);
} catch(NumberFormatException nfe) {
    // show error message like "The string ... you entered is not a number."
    ...
}

如果要将空字符串视为0(如果这对您的用例来说是合理的),您可以执行类似操作(仍需要try/catch块来捕获任何其他无效输入) :

try {
    long qq = 0;
    if (q != null && !q.isEmpty()) {
       qq = Long.parseLong(q);
    }

    ...

} catch(NumberFormatException nfe) {
    // show error message like "The string ... you entered is not a number."
    ...
}

除此之外,您应该检查代码的可变部分,并将其替换为变量,而不是复制代码,这样您就可以重用代码而不是重复代码。

答案 1 :(得分:2)

它引发了NumberFormatException,因为传递给parse()方法的参数是不可解析的。我的意思是说,这里的输入无效。

查看以下链接:) http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html

答案 2 :(得分:1)

输入的值似乎是一个空字符串:java.lang.NumberFormatException:对于输入字符串:“”
因此,在解析为long之前,您应该测试字符串值是否为空。 在任何方面,如果解析不成功,你应该捕获java.lang.NumberFormatException以设置默认值

答案 3 :(得分:1)

我们无法解析空格或null为long。如果我们尝试,则会出现数字格式异常。 所以在解析之前添加if语句来检查值是否存在。

if(q!=null && !"".equals(q))
qq=Long.parse(q);

在这些情况下使用异常处理很好