From b16c084024db4dc21b0e83e7af117822b50f0de6 Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Wed, 13 Aug 2025 18:23:46 +0200 Subject: [PATCH] Fix NullPointerException when creating cursor with null device #2414 This is a follow-up to e8aadbb8fa15ab03cfe7b972a5567eb2b36394ee which introduced a regression when working with null devices. If null is passed as argument, the value of Device.getDevice() is used as a fallback when initializing this field. This field must be used for all other operations in the constructor, rather than the constructor argument. Closes https://github.com/eclipse-platform/eclipse.platform.swt/issues/2414 --- .../win32/org/eclipse/swt/graphics/Cursor.java | 8 ++++---- .../tests/junit/Test_org_eclipse_swt_graphics_Cursor.java | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Cursor.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Cursor.java index 12a9c521adf..3450118afdc 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Cursor.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Cursor.java @@ -119,7 +119,7 @@ public final class Cursor extends Resource { public Cursor(Device device, int style) { super(device); this.cursorHandleProvider = new StyleCursorHandleProvider(style); - this.handle = this.cursorHandleProvider.createHandle(device, DEFAULT_ZOOM).getHandle(); + this.handle = this.cursorHandleProvider.createHandle(this.device, DEFAULT_ZOOM).getHandle(); init(); this.device.registerResourceWithZoomSupport(this); } @@ -160,7 +160,7 @@ public Cursor(Device device, int style) { public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int hotspotY) { super(device); this.cursorHandleProvider = new ImageDataWithMaskCursorHandleProvider(source, mask, hotspotX, hotspotY); - this.handle = this.cursorHandleProvider.createHandle(device, DEFAULT_ZOOM).getHandle(); + this.handle = this.cursorHandleProvider.createHandle(this.device, DEFAULT_ZOOM).getHandle(); init(); this.device.registerResourceWithZoomSupport(this); } @@ -229,7 +229,7 @@ private static CursorHandle setupCursorFromImageData(ImageData source, ImageData public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) { super(device); this.cursorHandleProvider = new ImageDataCursorHandleProvider(source, hotspotX, hotspotY); - this.handle = this.cursorHandleProvider.createHandle(device, DEFAULT_ZOOM).getHandle(); + this.handle = this.cursorHandleProvider.createHandle(this.device, DEFAULT_ZOOM).getHandle(); init(); this.device.registerResourceWithZoomSupport(this); } @@ -341,7 +341,7 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX, super(device); if (imageDataProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.cursorHandleProvider = new ImageDataProviderCursorHandleProvider(imageDataProvider, hotspotX, hotspotY); - this.handle = this.cursorHandleProvider.createHandle(device, DEFAULT_ZOOM).getHandle(); + this.handle = this.cursorHandleProvider.createHandle(this.device, DEFAULT_ZOOM).getHandle(); init(); this.device.registerResourceWithZoomSupport(this); } diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Cursor.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Cursor.java index cb8647a4a85..7c5f40d48a3 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Cursor.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Cursor.java @@ -154,6 +154,8 @@ public void test_ConstructorWithImageDataProvider() { Image sourceImage = new Image(display, 10, 10); Cursor cursor = new Cursor(display, sourceImage::getImageData, 0, 0); cursor.dispose(); + cursor = new Cursor(null, sourceImage::getImageData, 0, 0); + cursor.dispose(); sourceImage.dispose(); assertThrows("No exception thrown when ImageDataProvider is null",