Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » NullPointerException in Xtext IDE project after removing generate model statement from grammar(Grammar both imports existing EPackages and generates an EPackage)
NullPointerException in Xtext IDE project after removing generate model statement from grammar [message #1852034] Tue, 26 April 2022 17:58 Go to next message
Byron Trollip is currently offline Byron TrollipFriend
Messages: 2
Registered: April 2022
Junior Member
I am developing a grammar from an existing specification. Therefore I used some import statements in the grammar to import existing ecore EPackages (created from XML schemas) and implemented a simple grammar for some types in these existing EPackages. This grammar worked as expected.

Next, I wanted to benefit from Xtext's cross referencing functionality to improve some of the grammar. So I added a generate statement to create a new EPackage and added a new grammar rule that defines a cross reference. This grammar that combines imported and generated EPackages worked for my test.

However, this will not work for my use case due the requirement to only use the imported EPackages (existing specification), so I removed the generate statement, as well as my new grammar rule that I added. Unfortunately, this is where I ran into trouble.

My project is using Gradle as the build system. But, when I tried to build this grammar again (the one after I removed the generated EPackage), my basic parsing test failed during the build. So, I disabled this parsing test, to let the build finish for now. Then I starting debugging. I have created a run configuration for the Xtext Language Server in the project, so I can debug in Eclipse.

This error appears in the console log when I try to use content assist to fetch proposed auto completions in the editor (using keybind 'CTRL+SPACE' in Eclipse).

SEVERE: Internal error: java.lang.NullPointerException: parseResult is null
java.util.concurrent.CompletionException: java.lang.NullPointerException: parseResult is null
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:331)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:346)
	at java.base/java.util.concurrent.CompletableFuture$UniAccept.tryFire(CompletableFuture.java:704)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506)
	at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2088)
	at org.eclipse.xtext.ide.server.concurrent.AbstractRequest.logAndCompleteExceptionally(AbstractRequest.java:73)
	at org.eclipse.xtext.ide.server.concurrent.ReadRequest.lambda$doRun$0(ReadRequest.java:69)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException: parseResult is null
	at org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory.create(ContentAssistContextFactory.java:130)
	at org.eclipse.xtext.ide.server.contentassist.ContentAssistService.createProposals(ContentAssistService.java:104)
	at org.eclipse.xtext.ide.server.contentassist.ContentAssistService.createCompletionList(ContentAssistService.java:78)
	at org.eclipse.xtext.ide.server.LanguageServerImpl.lambda$completion$23(LanguageServerImpl.java:555)
	at org.eclipse.xtext.ide.server.WorkspaceManager.doRead(WorkspaceManager.java:438)
	at org.eclipse.xtext.ide.server.LanguageServerImpl.completion(LanguageServerImpl.java:554)
	at org.eclipse.xtext.ide.server.LanguageServerImpl.lambda$completion$22(LanguageServerImpl.java:540)
	at org.eclipse.xtext.ide.server.concurrent.ReadRequest.lambda$doRun$0(ReadRequest.java:66)
	... 5 more


This exception is being thrown in the org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory class after it checks that 'parseResult' is null.

public ContentAssistContext[] create(String document, ITextRegion selection, int offset, XtextResource resource) {
		this.document = document;
		this.selection = selection;
		this.resource = resource;
		//This is called to make sure late initialization is done.
		if (resource instanceof DerivedStateAwareResource) {
			resource.getContents();
		}
		// OP: This is the method call that returns null when I do not have a generate statement in my grammar
		this.parseResult = resource.getParseResult();
		if (parseResult == null)
			throw new NullPointerException("parseResult is null");
		return doCreateContexts(offset);
	}


I do not know why this error occurs now. I have tested with the grammar that generates a new EPackage and the parseResult is a valid AST containing my model. So the problem only occurs when I remove the 'generate' statement in the grammar.

To be clear, there are no build problems in the project and the Gradle clean build is completing successfully (after removing the parsing test). There are no errors compiling or running the project. However, when I interact with the Eclipse text editor which communicates with the Xtext Language Server, I get this runtime exception.

I do not know enough about Xtext or EMF to know why this is occurring.

For now, I have used a hack to get this project to work again, whereby I made a 'dummy' grammar rule that only has a single feature that references an existing grammar rule from an imported EPackage (this is a reference to an EDatatype that returns a string) Consequently, I have left the 'generate' statement in the grammar to allow this.

Also, simply having a generated model (EPackage) is not enough. I tried with a 'dummy' grammar rule that only references a terminal rule (f.e. ID), but it seems to want a link between the generated ecore and an imported ecore, else this 'parseResult' is null.

This 'dummy' grammar rule cannot be matched in a text by the parser, but simply having it exist is somehow required to get the parser to return a valid model for me. I'm out of ideas about why this is happening, and would like to know how to fix this properly, before I resort to starting over in a new Xtext project.

Installed versions:
Eclipse Xtext Xtext 2.26.0.v20211001-0541 org.eclipse.xtext.sdk
Eclipse Modeling Project EMF - Eclipse Modeling Framework Runtime and Tools 2.26.0.v20210506-1425 org.eclipse.emf
Eclipse Modeling Project EMF Ecore 2.24.0.v20210405-0628 org.eclipse.emf.ecore
Re: NullPointerException in Xtext IDE project after removing generate model statement from grammar [message #1852037 is a reply to message #1852034] Tue, 26 April 2022 19:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
do you register your epackage properly in MyDslIdeSetup/MyDslStandaloneSetup class?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: NullPointerException in Xtext IDE project after removing generate model statement from grammar [message #1852092 is a reply to message #1852037] Thu, 28 April 2022 16:47 Go to previous messageGo to next message
Byron Trollip is currently offline Byron TrollipFriend
Messages: 2
Registered: April 2022
Junior Member
Thanks for the response Christian. It helped me to pin down the problem. I am still learning EMF and Xtext, so please bear with me. I was not aware that I had to register the imported EPackages in the StandAloneSetup class when I do not have a generated model.

The console log from a Gradle clean build of the project has a line that I now notice seems to point to this problem.
[main] INFO lipse.emf.mwe.utils.StandaloneSetup - Using resourceSet registry. The registered Packages will not be registered in the global EPackage.Registry.INSTANCE!

However, I did not understand what this message meant.

I discovered an example in Chapter 13 of the reference book - Implementing Domain-Specific Languages with Xtext and Xtend (2nd Ed.) that I based a solution on.

In my project's StandAloneSetup class I added the following.

  @Override
  public void register(Injector injector) {
    if (!EPackage.Registry.INSTANCE.containsKey(MainPackage.eNS_URI)) {
      EPackage.Registry.INSTANCE.put(MainPackage.eNS_URI, MainPackage.eINSTANCE);
    }

    if (!EPackage.Registry.INSTANCE.containsKey(LoggingPackage.eNS_URI)) {
      EPackage.Registry.INSTANCE.put(LoggingPackage.eNS_URI, LoggingPackage.eINSTANCE);
    }

    if (!EPackage.Registry.INSTANCE.containsKey(StringutilPackage.eNS_URI)) {
      EPackage.Registry.INSTANCE.put(StringutilPackage.eNS_URI, StringutilPackage.eINSTANCE);
    }

    if (!EPackage.Registry.INSTANCE.containsKey(XmlmimePackage.eNS_URI)) {
      EPackage.Registry.INSTANCE.put(XmlmimePackage.eNS_URI, XmlmimePackage.eINSTANCE);
    }

    super.register(injector);
  }


This seems sensible to me, after looking into the way that the StandaloneSetupGenerated class registered my generated EPackage previously. However, I am still confused as to why I did not have to register the imported ecore EPackages in StandaloneSetup previously (when I had a generated EPackage).

Is this because the genmodel for the generated ecore could resolve the 'missing links' to the imported ecore models itself?
Re: NullPointerException in Xtext IDE project after removing generate model statement from grammar [message #1852093 is a reply to message #1852092] Thu, 28 April 2022 17:22 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
I assume so yes

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:How to react on external changes of the AST
Next Topic:Multiple Xtext files and Same Ecore
Goto Forum:
  


Current Time: Sun Sep 25 08:20:41 GMT 2022

Powered by FUDForum. Page generated in 0.01701 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top