Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormatException

 
h hewitt
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


At the click of a Button I request that the values from my TextField objects be stored in some variables. For the int and double types I have converted the String from the TextField to the appropriate type, but when I run the prgram I get a NumberFormatException. Some of the values used as parameters in my Film class have been specified as primitive types. I thought converting a String to a primitive was enough.
 
h hewitt
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a varation on the code. It compiles but it gives a NumberFormatException. I am using NetBeans.


 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the right idea to convert a String to a primitive numeric type. It will help if you provide the full stack trace? It will indicate which line is causing the exception. Also, please tell us what you type in the textbox that causes this exception.

Layne
 
h hewitt
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The user presumably will type in a number because the label asks for a year in one case or a the length of a film in another case...
 
Kenneth Albertson
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there h. Layne asked you to provide the full stack trace. That means the error messages. That will tell you (and us) which line is causing the problem. Even if, for some reason, you can't understand the error messages, you still need to identify the problem line. You could do that by commenting out all but one getText() line, running the program, and then repeating that process with different lines, until you identified where the problem occurs.

What do you do once you know which line is causing the problem? Many errors, and probably most errors encountered by beginners, occur because the program statement/s do not mean or do what you think they do. So the problem is with your mistaken assumptions. To fix that problem you need to identify it. To identify it you need to carefully check you assumptions against what the program actually does.

So, in this case, once you know the error line:
  • What is being typed into the TextField ?
  • What STRING does the getText() return to your program ?
  • Is that what you expected ?
  • What happens when you try to parse that as a number ?
  • Is that what you expected ?
  • If you still don't understand the problem (which you probably will by now), repeat this process with a different input.

    If you are still having problems, now you are ready to ask for help. Tell us what you have learnt (error messages, problem line, input, string value, etc) and someone will be able to help you.
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by h hewitt:
    The user presumably will type in a number because the label asks for a year in one case or a the length of a film in another case...


    Well, it is a well known fact that users do unexpected things. So what does the user ACTUALLY do? In other words, what is the EXACT String typed in that causes the execption? Also, please take the time to answer the rest of my questions so that I can help you.

    Layne
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You could do that by commenting out all but one getText() line, running the program, and then repeating that process with different lines, until you identified where the problem occurs.


    Or you can just look at the error message. It tells you the line number where the error occurs. If you post the full error message, we will be able to help you more easily.

    Layne
     
    h hewitt
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OK, I have read all of your posts. I will find out how to do a StackTrace in NetBeans and get back to you. Thank you to all of who responded.
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by h hewitt:
    OK, I have read all of your posts. I will find out how to do a StackTrace in NetBeans and get back to you. Thank you to all of who responded.


    The stack trace is the error message that your program prints out when an exception occurs. Your program already does this so all you have to do is run your program and copy and paste the error message here.

    Layne
    [ October 09, 2005: Message edited by: Layne Lund ]
     
    henri henri
    Ranch Hand
    Posts: 115
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    init:
    deps-jar:
    compile-single:
    run-single:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:468)
    at java.lang.Integer.parseInt(Integer.java:497)
    at film_titles.AddTitleFrame.mouseClicked(AddTitleFrame.java:149)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:212)
    at java.awt.Component.processMouseEvent(Component.java:5491)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
    at java.awt.Component.processEvent(Component.java:5253)
    at java.awt.Container.processEvent(Container.java:1966)
    at java.awt.Component.dispatchEventImpl(Component.java:3955)
    at java.awt.Container.dispatchEventImpl(Container.java:2024)
    at java.awt.Component.dispatchEvent(Component.java:3803)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3901)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
    at java.awt.Container.dispatchEventImpl(Container.java:2010)
    at java.awt.Window.dispatchEventImpl(Window.java:1774)
    at java.awt.Component.dispatchEvent(Component.java:3803)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
     
    henri henri
    Ranch Hand
    Posts: 115
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24212
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OK, so what part don't you understand? If you call parseInt() on an empty String (in this case) you get an exception. You'd also get one if the user typed "lkhjskjdskjsh" or "three". What you have to do is catch this exception, and do something about it -- i.e.,

     
    henri henri
    Ranch Hand
    Posts: 115
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't understand how I can come up with an empty String if I have taken care to enter a number into the TextField that asks for a number during the tests I carried out. None of my TextField objects had empty String objects before clicking the JButton labeled "Accept". I understand how to catch exceptions, so I will try that and see what happens.
     
    henri henri
    Ranch Hand
    Posts: 115
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It works. Thank you to everyone who posted.
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So what did you do to fix it?

    And why did you post the code for NumberFormatException? We all have that if we want to look at it since it is part of the standard Java API.

    Layne
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic