-1

I have noticed a strange problem with my program that I just can not figure out. I have included the method that has been flagged up, and the Error that was given. What's confusing me is that I have duplicated this method about 40 times on other buttons, changing only the 'engtf4' to another source where needed. They work perfectly with no errors, yet are written exactly the same. Adding further confusion, the method is executed exactly as intended(even though the error message below is given)?

I have looked around the net for similar problems using keywords - java.lang.NumberFormatException: For input string: "", I did however notice that all the example sites I saw had reference to a particular value in qoute marks. For example, -java.lang.NumberFormatException: For input string: "2345". I would appreciate any help I could get, thanks.

METHOD SOURCE CODE:

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("");             
}
}

ERROR MESSAGE:

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)
mKorbel
  • 109,525
  • 20
  • 134
  • 319
dazbrad
  • 182
  • 1
  • 12
  • What is your q? Where do you get it from? Please post possible value of q – Nabin Sep 17 '14 at 11:33
  • 9
    `LINE 13086`- seriously? – Andreas Fester Sep 17 '14 at 11:33
  • 4
    "I have duplicated this method about 40 times on other buttons, changing only the 'engtf4' to another source where needed" - you could start with refactoring your code. It could make debugging easier. – stuXnet Sep 17 '14 at 11:34
  • 2
    Well, reading the error, it's clear that q is set to the empty string. So that would make it quite hard to convert it to a number. – rje Sep 17 '14 at 11:34
  • q is a string that holds a numerical value that is entered in by user. – dazbrad Sep 17 '14 at 11:35
  • @dazbrad it should contain an entered number. In this case, it doesn't. Or does your debugger tell you differently? – stuXnet Sep 17 '14 at 11:37
  • a user can buy an item by entering the quantity into the text field, q is the string variable that I have set aside for temporarily storing the quantity for when they do buy an item. – dazbrad Sep 17 '14 at 11:39
  • @dazbrad and in this case, q is equal to "" - there is nothing in it, so it can't be parsed to a `Long`. `engtf4` (whatever that is) seems to be empty at the moment of `mouseReleased` – stuXnet Sep 17 '14 at 11:41
  • @dazbrad you can either try to make a small (!!!), complete example to reproduce your problem, or post all your code on gist or something like that. Actually, I could take a look on it. – stuXnet Sep 17 '14 at 11:43
  • when I do enter a value into the textfield, the same error still comes up, but the method still executes fine. I understand the gist of what everyone is saying, im just unsure as to why an error says that no value exists, yet the quantity still gets deducted, which means that the value has to have been existant. Neither is the value cleared at any point? – dazbrad Sep 17 '14 at 11:47
  • @Andreas- yes, line 13086, this is my first program and everything apart from this has went well. Then I realised it could be broken down, got to big before that point, now im scared to change it. But yes, seriously, lol. – dazbrad Sep 17 '14 at 11:50
  • @dazbrad sorry, I meant the service gist: https://gist.github.com – stuXnet Sep 17 '14 at 11:52
  • 1
    @stuXnet, im using an old mobile phone as my source of internet, transfering data isnt that friendly with this phone. If I have no luck with the examples below I will pop round a mates and use their pc if they are in. Thanks for the option anyhow, much appreciated. Didnt expect so much of a response, thanks guys. – dazbrad Sep 17 '14 at 12:00
  • 1
    See http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Raedwald Sep 17 '14 at 12:54

4 Answers4

2

Long.parseLong() throws a NumberFormatException if the passed string is not a parsable long value. In your case, you are passing an empty string (probably because the user did not enter anything in the input field). You can verify this easily:

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

You should always be prepared to catch this exception (besides empty strings, the exception is thrown if the user enters anything which is not a Long), and show an appropriate error message to the user, something like

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

If you want to treat an empty string as 0 (if that is reasonable for your use case), you can do something like (still requires the try/catch block to catch any other invalid inputs):

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."
    ...
}

In addition to that, instead of duplicating code dozens of times, you should check which are the variable parts of your code and replace them with variables, so that you can reuse the code instead of duplicating it.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • For the method to execute correctly, a value must have been passed. Without doubting your solutiin I thought that my code had to work. Turns out that it wasnt the problem, the reason that it kicked up an error was due to the fact that my mouselistener was pointed at another button entirely. A clear example of why having so much code in one place is a bad thing, I would have seen that much easier had I not coded so many lines. Thanks for the help anyway. – dazbrad Sep 17 '14 at 12:30
2

It is raising the NumberFormatException, Since the argument passed to the parse() Method, is not parsable. What i mean to say is, input is invalid here.

Checkout the following link :) http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html

Kamesh
  • 1,435
  • 1
  • 14
  • 27
1

It seems that the value entered is an empty string :java.lang.NumberFormatException: For input string: ""
So before parsing to long you should test if the string value is empty or not. In any ways you should catch the java.lang.NumberFormatException to put a default value if the parse doesn't succeed

baki
  • 95
  • 5
1

We cannot parse empty space or null to long.If we try that,the number format exception arise. so before parsing add the if statement to check the value is present or not.

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

It's good to use exception handling in these situations

Selva
  • 1,620
  • 3
  • 33
  • 63