-1

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)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    How can `null.equals(null)`? You exception is telling you that the input is empty (`""`) not `null` – MadProgrammer Jun 22 '22 at 20:26
  • 1
    `getText().trim().isEmpty()` or `getText().trim().isBlank()` might be a better choice. You also still need to be prepared for the possibility that the text is not converted to a `int`. You might consider using a `JFormattedTextField` or `JSpinner` instead – MadProgrammer Jun 22 '22 at 20:28
  • if `getText()` returns null, then the call to `trim()` will throw a NullPointerException. `trim()` will never return null. – Michael Jun 22 '22 at 20:31
  • For all objects, `object.equals(null)` is *always* false. No object is ever equal to null. As MadProgrammer said, you want to call isEmpty(), not equals(null). – VGR Jun 22 '22 at 23:16

2 Answers2

1

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);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

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)
Jordan
  • 11
  • 5
  • 2
    `isBlank` is `isEmpty` would be better candidates, but, you'd still need to deal with the possibility that the input is not convertible to an `int`, so, actually a `try-catch` might be a overall better solution – MadProgrammer Jun 22 '22 at 20:30
  • Agreed, OP should check out https://stackoverflow.com/a/1486082/5343827 for handling parsing their XY coordinates if they are determined to be checking for nulls. – Jordan Jun 22 '22 at 20:36
  • Thanks, I'll wait. Suddenly someone else wants to leave their answer. –  Jun 22 '22 at 21:04