Skip to content

NPE when painting "empty" image #3442

Description

@ptziegler

Describe the bug

Trying to paint an "empty" image (an image for which no GC was created) fails with a NullPointerException.

To Reproduce

package gef;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class DrawGC2 {
	public static void main(String[] args) {
		Shell shell = new Shell();
		Display display = shell.getDisplay();

		int width = 310;
		int height = 310;

		Image image = new Image(display, width, height);

		shell.addPaintListener(event -> {
			event.gc.drawImage(image, 0, 0, width, height, 0, 0, 100, 100);
		});

		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		image.dispose();
	}
}

Stacktrace:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Optional.orElse(Object)" is null
	at org.eclipse.swt.graphics.Image$AbstractImageProviderWrapper.getClosestAvailableImageData(Image.java:2105)
	at org.eclipse.swt.graphics.Image$PlainImageProviderWrapper.loadImageData(Image.java:2388)
	at org.eclipse.swt.graphics.Image$HandleAtSize.getOrCreateImageHandleAtClosestSize(Image.java:254)
	at org.eclipse.swt.graphics.Image$HandleAtSize.lambda$0(Image.java:225)
	at java.base/java.util.Optional.orElseGet(Optional.java:364)
	at org.eclipse.swt.graphics.Image$HandleAtSize.refresh(Image.java:225)
	at org.eclipse.swt.graphics.Image.executeOnImageHandleAtBestFittingSize(Image.java:968)
	at org.eclipse.swt.graphics.GC$DrawScalingImageToImageOperation.draw(GC.java:1240)
	at org.eclipse.swt.graphics.GC$DrawScalingImageToImageOperation.apply(GC.java:1222)
	at org.eclipse.swt.graphics.GC.storeAndApplyOperationForExistingHandle(GC.java:6136)
	at org.eclipse.swt.graphics.GC.drawImage(GC.java:1152)
	at gef.DrawGC2.lambda$0(DrawGC2.java:18)
	at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:272)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:91)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4363)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1217)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1241)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1226)
	at org.eclipse.swt.widgets.Composite.WM_PAINT(Composite.java:1627)
	at org.eclipse.swt.widgets.Control.windowProc(Control.java:5021)
	at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:336)
	at org.eclipse.swt.widgets.Decorations.windowProc(Decorations.java:1490)
	at org.eclipse.swt.widgets.Shell.windowProc(Shell.java:2446)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:5131)
	at org.eclipse.swt.internal.win32.OS.UpdateWindow(Native Method)
	at org.eclipse.swt.widgets.Decorations.setVisible(Decorations.java:1322)
	at org.eclipse.swt.widgets.Shell.setVisible(Shell.java:2192)
	at org.eclipse.swt.widgets.Shell.open(Shell.java:1334)
	at gef.DrawGC2.main(DrawGC2.java:21)

Expected behavior
Either a "blank" image should be drawn or an SWTError be thrown.

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS
  1. JRE/JDK version

Version since

Only tested with the master.

Metadata

Metadata

Assignees

No one assigned

    Labels

    WindowsHappens on Windows OS

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions