Fix NPEs in MenuItem#calculateRenderSize() and MenuItem#getMonitorZoom() #2357
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Description
In
MenuItem#calculateRenderSize, a GC (graphics context) is created usingthis.getMenu().getShell(). However, this assumes that the menu returned bygetMenu()is non-null, which is only valid for MenuItems with styleSWT.CASCADEthat have an explicitly assigned submenu.For MenuItems with styles like
SWT.PUSH,SWT.CHECK, orSWT.RADIO, the result ofgetMenu()is null, as these items do not have submenus. Attempting to callgetShell()on a null Menu leads to aNullPointerExceptionduring size computation.To address this, we replace:
GC gc = new GC(this.getMenu().getShell());with:
GC gc = new GC(this.getParent().getShell());This ensures that the GC is always created using the Shell associated with the parent Menu, which is guaranteed to exist, regardless of the MenuItem style.
How to Reproduce
In order to reproduce, run the runtime workspace with following plug-in added to your configuration:
org.eclipse.ui.examples.contributionsand add these two menuItem in menucontributionPreviously, you would have got this error:
Expected result
No error should appear in console.
fixes: #2247