Results 1 to 4 of 4
  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Rep Power

    Default why does netbeans hate my "if" statement?

    I was just messing around in Java and I was trying to have my UI such that if anyone types a number into the second textbox, the program will follow an "if" statement.

    as I have it now the "if" statement is set up like
    Java Code:
    double k = Double.parseDouble(jTextField1.getText());
    K is defined as the number that the user put in
    Java Code:
    I'm trying to say while the second textfield is blank just ignore it. But if someone types a number into it then follow the "if" statement. Thus if K is NOT null(blank) the If statement should kick in and run.

    I keep getting an error "Incompatible types Double and Null". I figured, okay I'll just set the statement to 0 then!...
    Java Code:
    the error went away but it didn't work properly. instead I got more errors when the program was running.

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa
    at java.lang.Double.parseDouble(
    at evansinterface.mainWindow.jButton1ActionPerformed(
    at evansinterface.mainWindow.access$1000(mainWindow.j ava:27)
    at evansinterface.mainWindow$11.actionPerformed(mainW
    at javax.swing.AbstractButton.fireActionPerformed(Abs
    at javax.swing.AbstractButton$Handler.actionPerformed (
    at javax.swing.DefaultButtonModel.fireActionPerformed (
    at javax.swing.DefaultButtonModel.setPressed(DefaultB
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(
    at java.awt.Component.processMouseEvent(Component.jav a:6516)
    at javax.swing.JComponent.processMouseEvent(JComponen
    at java.awt.Component.processEvent( 1)
    at java.awt.Container.processEvent( 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4872)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent( 98)
    at java.awt.LightweightDispatcher.retargetMouseEvent(
    at java.awt.LightweightDispatcher.processMouseEvent(C
    at java.awt.LightweightDispatcher.dispatchEvent(Conta
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl( )
    at java.awt.Component.dispatchEvent( 98)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
    at java.awt.EventQueue.access$200( )
    at java.awt.EventQueue$
    at java.awt.EventQueue$
    at Method)
    at$1.doIntersectionPri vilege(
    at$1.doIntersectionPri vilege(
    at java.awt.EventQueue$
    at java.awt.EventQueue$
    at Method)
    at$1.doIntersectionPri vilege(
    at java.awt.EventQueue.dispatchEvent( 705)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(
    at java.awt.EventDispatchThread.pumpEventsForFilter(E
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(
    at java.awt.EventDispatchThread.pumpEvents(EventDispa
    at java.awt.EventDispatchThread.pumpEvents(EventDispa

    What am I doing wrong here?
    Last edited by innovo; 08-11-2015 at 03:58 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: why does netbeans hate my "if" statement?

    double is a primitive so the comparison to null makes no sense. The argument to parseDouble method *must* be a number or a NumberFormatException will be thrown.

    Perhaps you should first check to see if the text is actually a number. You could check the text for equality to a space or an empty string.

    Last edited by jim829; 08-11-2015 at 04:00 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: why does netbeans hate my "if" statement?

    Also consider that a user could enter a non-numeric value in a text field.
    To catch that, wrap the parse method in a try{}catch block to catch invalid input.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: why does netbeans hate my "if" statement?

    Ah, but what if the user inputs TWO spaces? ;) Perhaps the String class has a method that can trim whitespace from it before you check if it is empty.

    String (Java Platform SE 7 )

    (disclaimer: link to Java 7 docs because the CSS is just better).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 3
    Last Post: 01-30-2014, 08:21 AM
  2. Replies: 0
    Last Post: 10-12-2013, 10:24 AM
  3. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  4. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  5. Replies: 1
    Last Post: 01-21-2010, 09:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts