Skip to main content

NullPointerException in processMouseMotionEvent

2 replies [Last post]
boomah
Offline
Joined: 2007-07-13
Points: 0

Hi all.

I'm extending AbstractLayerUI and overriding the processMouseMotionEvent method.

I call getMousePosition on the JXLayer that is passed into this method and very occasionally in a non repeatable way a NullPointerException is thrown.

Here's the stack:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Component.findUnderMouseInWindow(Component.java:1249)
at java.awt.Component.getMousePosition(Component.java:1297)
at starling.pivot.view.swing.PivotTableLayerUI.processMouseMotionEvent(PivotTableLayerUI.scala:223)
at org.jdesktop.jxlayer.plaf.AbstractLayerUI.eventDispatched(Unknown Source)
at org.jdesktop.jxlayer.JXLayer$LayerEventController.eventDispatched(Unknown Source)
at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2353)
at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Toolkit.java:2245)
at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Toolkit.java:2244)
at java.awt.Toolkit.notifyAWTEventListeners(Toolkit.java:2203)
at java.awt.Component.dispatchEventImpl(Component.java:4528)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4255)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Have I set something up incorrectly or have a found a bug?

Thanks...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pietblok
Offline
Joined: 2003-07-17
Points: 0

Hi boomah,

Cutious as I am with respect to MouseEvents, I looked the code up in the sources (jdk6 update 20, because the line numbers seem to match)

first Component.getMousePosition()

[code]
public Point getMousePosition() throws HeadlessException {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}

PointerInfo pi = (PointerInfo)java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
return MouseInfo.getPointerInfo();
}
}
);

synchronized (getTreeLock()) {
Component inTheSameWindow = findUnderMouseInWindow(pi); // This is line 1297
if (!isSameOrAncestorOf(inTheSameWindow, true)) {
return null;
}
return pointRelativeToComponent(pi.getLocation());
}
}
[/code]

Now Component.findUnderMouseInWindow()

[code]
Component findUnderMouseInWindow(PointerInfo pi) {
if (!isShowing()) {
return null;
}
Window win = getContainingWindow();
if (!Toolkit.getDefaultToolkit().getMouseInfoPeer().isWindowUnderMouse(win)) {
return null;
}
final boolean INCLUDE_DISABLED = true;
Point relativeToWindow = win.pointRelativeToComponent(pi.getLocation()); // This is line 1249
Component inTheSameWindow = win.findComponentAt(relativeToWindow.x,
relativeToWindow.y,
INCLUDE_DISABLED);
return inTheSameWindow;
}
[/code]

So, in line 1249 either [b]win[/b] or [b]pi[/b] is null.

Can win be null? I doubt it, because in a statement earlier a test is made with win as an argument. Were it null, it would not have returned true I guess.

Then the pi. It is obtained from MouseInfo.getPointerInfo() in Component.getMousePosition())

[code]
public static PointerInfo getPointerInfo() throws HeadlessException {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}

SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(SecurityConstants.WATCH_MOUSE_PERMISSION);
}

Point point = new Point(0, 0);
int deviceNum = Toolkit.getDefaultToolkit().getMouseInfoPeer().fillPointWithCoords(point);
GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().
getScreenDevices();
PointerInfo retval = null;
if (areScreenDevicesIndependent(gds)) {
retval = new PointerInfo(gds[deviceNum], point);
} else {
for (int i = 0; i < gds.length; i++) {
GraphicsConfiguration gc = gds[i].getDefaultConfiguration();
Rectangle bounds = gc.getBounds();
if (bounds.contains(point)) {
retval = new PointerInfo(gds[i], point);
}
}
}

return retval;
}
[/code]

Can retval be left as null? Yes, when none of the bounds contains point (0,0).
Do you have "dependent screen devices" (whatever that may be)?

It smells to me like a bug (in Component or MouseInfo) but of course I'm just an amateur. Let's see what the real experts have to say.

Thanks,
Piet

boomah
Offline
Joined: 2007-07-13
Points: 0

Hi Piet. Thanks for digging deeper.

I'm not sure what is meant by "dependent screen devices".

The problem I've got is I can't reproduce the error at all on my machine.

My colleague who alerted me to the problem can produce the error on his machine occasionally, but certainly not all the time.

The only difference in environments is he is using update 20 and I'm using update 19.

I'll upgrade to 20 and try and reproduce it on my machine.

FYI The error occurs when using Ubuntu 9.10 64bit, sun java update 20 64bit.