Mouse location throws NullPointerException

Bug #2060175 reported by Peter Zejda
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SikuliX
New
Undecided
Unassigned

Bug Description

1. The version of Sikuli you were using.
  2.0.5
2. Your operating system, 32-bit or 64-bit, and its version.
  MS Windows 11, 64-bit
3. The procedure to reproduce the bug.
  Mouse.at().getPoint()
4. Any information that might help us to locate the bug.
  See below :-)

I'm using Jython with Sikulix, and am trying to get information about the Mouse location with a call such as this:

  mouse_off_scr = scr.getBounds().contains(Mouse.at().getPoint())

However, this frequently results in a NullPointerException being thrown within the Java library, and (now sure why) I can't even catch the error with a try/except BaseException. I did a bit of digging into the Java code, and found the culprit within the Device class method getLocation().

  public Location getLocation() {
    PointerInfo mp = MouseInfo.getPointerInfo();
    if (mp != null) {
      return new Location(MouseInfo.getPointerInfo().getLocation());
    } else {
      Debug.error("Mouse: not possible to get mouse position (PointerInfo == null)");
      return null;
    }
  }

It looks like there is some code in place to avoid the NPE. Even though it's checking for a null return value from MouseInfo.getPointerInfo(), the method gets called a second time and that's when the NPE occurs. I think the method should probably be corrected as follows:

  public Location getLocation() {
    PointerInfo mp = MouseInfo.getPointerInfo();
    if (mp != null) {
      return new Location(mp.getLocation());
    } else {
      Debug.error("Mouse: not possible to get mouse position (PointerInfo == null)");
      return null;
    }
  }

That may result is very slightly "outdated" mouse information, but at least it would avoid crashing the program. :-)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.