Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 357176

Summary: Potential NPE in AttributedPosition#createStyleRange()
Product: [Modeling] TMF Reporter: Karsten Thoms <karsten.thoms>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: minor    
Priority: P3 CC: knut.wannheden, sebastian.zarnekow
Version: 2.0.1Flags: sebastian.zarnekow: juno+
Target Milestone: M4   
Hardware: PC   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description Karsten Thoms CLA 2011-09-08 20:39:16 EDT
It can occur that the variable 'attribute' is null, which leads to a NPE in line
   int style= textAttribute.getStyle();

This happened to me when I pasted contents into a code template in the templates preference page.

Full Stack:
Daemon Thread [Thread-1] (Suspended (exception NullPointerException))	
	AttributedPosition.createStyleRange() line: 44	
	HighlightingPresenter.applyTextPresentation(TextPresentation) line: 604	
	XtextSourceViewer(TextViewer).changeTextPresentation(TextPresentation, boolean) line: 4877	
	HighlightingPresenter.updatePresentation(TextPresentation, AttributedPosition[], AttributedPosition[]) line: 488	
	HighlightingPresenter$1.run() line: 370	
	RunnableLock.run() line: 35	
	UISynchronizer(Synchronizer).runAsyncMessages(boolean) line: 135	
	Display.runAsyncMessages(boolean) line: 3935	
	Display.readAndDispatch() line: 3612	
	EditTemplateDialog(Window).runEventLoop(Shell) line: 825	
	EditTemplateDialog(Window).open() line: 801	
	AdvancedTemplatesPreferencePage.editTemplate(Template, boolean, boolean) line: 56	
	AdvancedTemplatesPreferencePage(TemplatePreferencePage).edit(TemplatePersistenceData) line: 1271	
	AdvancedTemplatesPreferencePage(TemplatePreferencePage).edit() line: 1266	
	TemplatePreferencePage.access$1(TemplatePreferencePage) line: 1258	
	TemplatePreferencePage$16.handleEvent(Event) line: 975	
	EventTable.sendEvent(Event) line: 84	
	Display.sendEvent(EventTable, Event) line: 4125	
	Button(Widget).sendEvent(Event) line: 1457	
	Button(Widget).sendEvent(int, Event, boolean) line: 1480	
	Button(Widget).sendEvent(int, Event) line: 1465	
	Button(Widget).notifyListeners(int, Event) line: 1270	
	Display.runDeferredEvents() line: 3971	
	Display.readAndDispatch() line: 3610	
	WorkbenchPreferenceDialog(Window).runEventLoop(Shell) line: 825	
	WorkbenchPreferenceDialog(Window).open() line: 801	
	WorkbenchPreferenceDialog.open() line: 215	
	ShowPreferencePageHandler.execute(ExecutionEvent) line: 54	
	HandlerProxy.execute(ExecutionEvent) line: 293	
	Command.executeWithChecks(ExecutionEvent) line: 476	
	HandlerService.executeCommand(String, Event) line: 178	
	CocoaUIEnhancer.runCommand(String) line: 393	
	CocoaUIEnhancer.actionProc(long, long, long) line: 535	
	OS.objc_msgSendSuper(objc_super, long, long) line: not available [native method]	
	Display.applicationSendEvent(long, long, long) line: 4986	
	Display.applicationProc(long, long, long) line: 5135	
	OS.objc_msgSend(long, long, long) line: not available [native method]	
	NSApplication.sendEvent(NSEvent) line: 128	
	Display.readAndDispatch() line: 3607	
	Workbench.runEventLoop(Window$IExceptionHandler, Display) line: 2696	
	Workbench.runUI() line: 2660	
	Workbench.access$4(Workbench) line: 2494	
	Workbench$7.run() line: 674	
	Realm.runWithDefault(Realm, Runnable) line: 332	
	Workbench.createAndRunWorkbench(Display, WorkbenchAdvisor) line: 667	
	PlatformUI.createAndRunWorkbench(Display, WorkbenchAdvisor) line: 149	
	IDEApplication.start(IApplicationContext) line: 123	
	EclipseAppHandle.run(Object) line: 196	
	EclipseAppLauncher.runApplication(Object) line: 110	
	EclipseAppLauncher.start(Object) line: 79	
	EclipseStarter.run(Object) line: 344	
	EclipseStarter.run(String[], Runnable) line: 179	
	NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]	
	NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39	
	DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25	
	Method.invoke(Object, Object...) line: 597	
	Main.invokeFramework(String[], URL[]) line: 622	
	Main.basicRun(String[]) line: 577	
	Main.run(String[]) line: 1410	
	Main.main(String[]) line: 1386
Comment 1 Sebastian Zarnekow CLA 2011-11-09 14:51:24 EST
Not 2.1
Comment 2 Knut Wannheden CLA 2011-11-10 09:18:33 EST
FWIW I've come across this problem as well:

org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.NullPointerException)
	at org.eclipse.swt.SWT.error(SWT.java:4282)
	at org.eclipse.swt.SWT.error(SWT.java:4197)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:138)
	at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
	at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
	at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
	at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
Caused by: java.lang.NullPointerException
	at org.eclipse.xtext.ui.editor.syntaxcoloring.AttributedPosition.createStyleRange(AttributedPosition.java:48)
	at org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingPresenter.applyTextPresentation(HighlightingPresenter.java:604)
	at org.eclipse.jface.text.TextViewer.changeTextPresentation(TextViewer.java:4877)
	at org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingPresenter.updatePresentation(HighlightingPresenter.java:488)
	at org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingPresenter$1.run(HighlightingPresenter.java:370)
	at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
	... 23 more
Comment 3 Sebastian Zarnekow CLA 2011-11-10 16:22:20 EST
Karsten, can you provide some more informatione, e.g. which language the code templates belong to and how the code snippet looked like?
Comment 4 Karsten Thoms CLA 2011-11-10 16:55:46 EST
I am getting this one in my Spray Runtime workspace.

  git clone https://code.google.com/a/eclipselabs.org/p/spray/
  tag "bug357176"

Reproduce:
- open Preferences -> Spray -> Templates
- select template "Line"

The NPE occurs for me when selecting this template
Comment 5 Sebastian Zarnekow CLA 2011-11-10 17:04:04 EST
(In reply to comment #4)
> I am getting this one in my Spray Runtime workspace.
> 
>   git clone https://code.google.com/a/eclipselabs.org/p/spray/
>   tag "bug357176"
> 
> Reproduce:
> - open Preferences -> Spray -> Templates
> - select template "Line"
> 
> The NPE occurs for me when selecting this template

Does the problem persist if you disable the layout_id token style, e.g. by replacing it by the default keyword style?
Comment 6 Karsten Thoms CLA 2011-11-11 11:34:37 EST
I think you meant in
   SprayHighlightingConfiguration#configure

I have commented out all custom code here. The error still remains.
Comment 7 Sebastian Zarnekow CLA 2011-11-11 11:35:27 EST
I was refering to the token colorer.
Comment 8 Karsten Thoms CLA 2011-11-15 17:16:20 EST
I have now checked this. The NPE does disappears when the SprayTokenToAttributeIdMapper#calculateId() does not return LAYOUT_ID

    protected String calculateId(String tokenName, int tokenType) {
        if (layoutKeywords.contains(tokenName)) {
            return SprayHighlightingConfiguration.LAYOUT_ID;
        }
        return super.calculateId(tokenName, tokenType);
    }

When I comment out the if block the error does not appear.
Comment 9 Sebastian Zarnekow CLA 2011-11-17 08:23:16 EST
Pushed a NPE guard to master. Custom highlighting styles don't work at the moment for the template configuration.
Comment 10 Karsten Thoms CLA 2017-09-19 17:22:18 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 11 Karsten Thoms CLA 2017-09-19 17:33:42 EDT
Closing all bugs that were set to RESOLVED before Neon.0