wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes NumberFormatException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NumberFormatException" Watch "NumberFormatException" New topic
Author

NumberFormatException

h hewitt
Greenhorn

Joined: Oct 09, 2005
Posts: 4


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

Joined: Oct 09, 2005
Posts: 4
Here is a varation on the code. It compiles but it gives a NumberFormatException. I am using NetBeans.


Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
h hewitt
Greenhorn

Joined: Oct 09, 2005
Posts: 4
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

Joined: Sep 18, 2005
Posts: 59
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

    Joined: Dec 06, 2001
    Posts: 3061
    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

    Joined: Dec 06, 2001
    Posts: 3061
    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

    Joined: Oct 09, 2005
    Posts: 4
    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

    Joined: Dec 06, 2001
    Posts: 3061
    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

    Joined: Oct 03, 2005
    Posts: 115
    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

    Joined: Oct 03, 2005
    Posts: 115
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24183
        
      34

    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.,



    [Jess in Action][AskingGoodQuestions]
    henri henri
    Ranch Hand

    Joined: Oct 03, 2005
    Posts: 115
    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

    Joined: Oct 03, 2005
    Posts: 115
    It works. Thank you to everyone who posted.
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: NumberFormatException