Home > Back-end >  Why is the condition txtfield.getText().trim().equals(null) not executed if it is true?
Why is the condition txtfield.getText().trim().equals(null) not executed if it is true?

Time:06-23

So, I'm writing an autoclicker. I need to check whether the user entered the cursor coordinates or not. To do this, I use text boxes. To check, I check for text. If there is text then condition 1 is triggered. If there is no text, then condition 1 does not work, but neither does 2.

//Condition 1
if(!Window.x.getText().trim().equals(null) && !Window.y.getText().trim().equals(null)) {
    x = Integer.parseInt(Window.x.getText().trim());
    System.out.println("X - OK");
    y = Integer.parseInt(Window.y.getText().trim());
    System.out.println("Y - OK");
//Condition 2   
}else if(Window.x.getText().trim().equals(null) || Window.y.getText().trim().equals(null)) {
    JOptionPane.showMessageDialog(null, "Please specify cursor coordinates(X, Y)", "ERROR", JOptionPane.ERROR_MESSAGE);
    return;
}

Error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at com.npesoftware.nautoclicker.Clicker.check(Clicker.java:29)
    at com.npesoftware.nautoclicker.Window$1.actionPerformed(Window.java:93)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6379)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

CodePudding user response:

You should be checking if the string is empty or contains blank space as well as being null. If the length is > 0, it means there is nonwhite text in the textbox.

!(Window.x.getText().trim().length() > 0) && !(Window.y.getText().trim().length() > 0)

CodePudding user response:

null.equals(null) makes no sense. The condition would better be stated as null == Window.x.getText(), but assuming you're using a JTextField, you're guaranteed not to get a null value.

Instead you should be using String#isEmpty or in this context String#isBlank

This, however, does not mean that the text you get will be convertible to a int. In this case, you could make use of JFormattedTextField or JSpinner to restrict the input of the user.

But, you'd probably still be safer to wrap the conversation in a try-catch block anyway...

try {
    int x = Integer.parseInt(Window.x.getText().trim());
    int y = Integer.parseInt(Window.y.getText().trim());
} catch (NumberFormatException exp) {
    JOptionPane.showMessageDialog(null, "Illegal coordinates", "Error", JOptionPane.ERROR_MESSAGE);
}
  • Related