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

    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:
    if(k!=null){
                System.out.println("WOO");
                   }
    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:
    if(k!=0){
                System.out.println("WOO");
                   }
    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 tingDecimal.java:1020)
    at java.lang.Double.parseDouble(Double.java:540)
    at evansinterface.mainWindow.jButton1ActionPerformed( mainWindow.java:415)
    at evansinterface.mainWindow.access$1000(mainWindow.j ava:27)
    at evansinterface.mainWindow$11.actionPerformed(mainW indow.java:239)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6516)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3320)
    at java.awt.Component.processEvent(Component.java:628 1)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4872)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 98)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 98)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 705)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)


    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
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    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.

    Regards,
    Jim
    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
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    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
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    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
  •