Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » NullPointerException while invoking JvmModelInferrer/Generator for code generation from a Java Proje(NullPointerException while invoking JvmModelInferrer/Generator for code generation from a Java Project)
NullPointerException while invoking JvmModelInferrer/Generator for code generation from a Java Proje [message #1252343] Fri, 21 February 2014 05:44 Go to next message
Priya Sabut is currently offline Priya SabutFriend
Messages: 41
Registered: September 2012
Member
I am working on a project which has a requirement to generate Java class depending on the language(the language will have only function definitions) specified. And the JvmModelInferrer/IGenerater needed to called from a java project implicitly.

Followed the steps given in post http://stackoverflow.com/questions/11341342/how-to-invoke-xtext-parser-jvmmodelinferrer-from-a-java-project, but unfortunately EclipseResourceFileSystemAccess2.generateFile() is throwing NullPointerException.

The code used to generate:

public class Generator { private static IGenerator generator;
public static void main(String[] args) {
// this line registers the EMF for our DSL
Injector injector = new RequestDslStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = new ResourceSetImpl();
File file = new File(
"D:/Xtext/XtextFormIntegration/runtime-XtextFormNC/XtextFormCar/lang.request");
Resource resource = rs.getResource(URI.createURI(file.toURI().toString()), true);

//setup the generator
generator = injector.getInstance(IGenerator.class);

//setup the file system access
final EclipseResourceFileSystemAccess2 fsa = new EclipseResourceFileSystemAccess2();

generator.doGenerate(resource, fsa);
}
}

The exception is:

Exception in thread "main" java.lang.NullPointerException
at org.eclipse.xtext.builder.EclipseResourceFileSystemAccess2.generateFile(EclipseResourceFileSystemAccess2.java:156)
at org.eclipse.xtext.generator.AbstractFileSystemAccess.generateFile(AbstractFileSystemAccess.java:75)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._internalDoGenerate(JvmModelGenerator.java:201)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.internalDoGenerate(JvmModelGenerator.java:1756)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.doGenerate(JvmModelGenerator.java:181)
at de.itemis.utils.jface.viewers.Generator.main(Generator.java:41)


Need your help in resolving the above mentioned issue. I wonder what am I missing!!

Thanks in advance.
Re: NullPointerException while invoking JvmModelInferrer/Generator for code generation from a Java P [message #1252390 is a reply to message #1252343] Fri, 21 February 2014 06:56 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
When running outside eclipse, you need to use the JavaIOFileSystemAccess.
Also: Pleaseprovide the version number of Xtext you are using in future posts.
Re: NullPointerException while invoking JvmModelInferrer/Generator for code generation from a Java P [message #1252392 is a reply to message #1252343] Fri, 21 February 2014 06:57 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13641
Registered: July 2009
Senior Member
Use the injector you get from createinjectoranddoemfregistration to
get an instance of the access

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


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 while invoking JvmModelInferrer/Generator for code generation from a Java P [message #1252636 is a reply to message #1252392] Fri, 21 February 2014 12:32 Go to previous messageGo to next message
Priya Sabut is currently offline Priya SabutFriend
Messages: 41
Registered: September 2012
Member
Hi Sven & Christian,
Thanks for your reply. I am able to generate java file after using JavaIOFileSystemAccess and injecting EncodingProvider as mentioned by Christian in post http://www.eclipse.org/forums/index.php/t/628292/

I am using XTEXT 2.4.1.

But when I use same steps for code generation where I am using the XtextStyledTextAdapter for xtext-form integration as specified in Yakindu project I am getting ClassCastException. I am calling generateCode() on a button click

private void generateCode(){
Injector injector = new RequestDslStandaloneSetup().createInjectorAndDoEMFRegistration();

XtextResource resource = xtextAdapter.getFakeResourceContext().getFakeResource();

//setup the generator
generator = injector.getInstance(IGenerator.class);

//setup the file system access
final JavaIoFileSystemAccess jfsa = new JavaIoFileSystemAccess();

jfsa.setOutputPath("abc");
Guice.createInjector(new AbstractGenericModule() {

public Class<? extends IEncodingProvider> bindIEncodingProvider() {
return IEncodingProvider.Runtime.class;
}

}).injectMembers(jfsa);

generator.doGenerate(resource, jfsa);

}
The exception I am getting :

java.lang.ClassCastException: org.eclipse.xtext.common.types.access.jdt.JdtTypeProvider cannot be cast to org.eclipse.xtext.common.types.access.impl.ClasspathTypeProvider
at org.eclipse.xtext.common.types.access.ClasspathTypeProviderFactory.findTypeProvider(ClasspathTypeProviderFactory.java:63)
at org.eclipse.xtext.common.types.access.ClasspathTypeProviderFactory.findTypeProvider(ClasspathTypeProviderFactory.java:1)
at org.eclipse.xtext.common.types.access.AbstractTypeProviderFactory.findOrCreateTypeProvider(AbstractTypeProviderFactory.java:38)
at org.eclipse.xtext.common.types.util.TypeReferences.findDeclaredType(TypeReferences.java:233)
at org.eclipse.xtext.common.types.util.TypeReferences.findDeclaredType(TypeReferences.java:212)
at org.eclipse.xtext.common.types.util.TypeReferences.getTypeForName(TypeReferences.java:178)
at org.eclipse.xtext.xbase.typing.JvmExceptions.findUnhandledExceptions(JvmExceptions.java:39)
at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.needsSneakyThrow(AbstractXbaseCompiler.java:254)
at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.compile(AbstractXbaseCompiler.java:232)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateExecutableBody(JvmModelGenerator.java:1133)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._generateMember(JvmModelGenerator.java:831)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateMember(JvmModelGenerator.java:1801)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator$2.apply(JvmModelGenerator.java:286)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator$2.apply(JvmModelGenerator.java:1)
at org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_doubleArrow(ObjectExtensions.java:139)
at org.eclipse.xtext.xbase.compiler.LoopExtensions.forEach(LoopExtensions.java:34)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._generateBody(JvmModelGenerator.java:290)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateBody(JvmModelGenerator.java:1773)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateType(JvmModelGenerator.java:209)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._internalDoGenerate(JvmModelGenerator.java:200)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.internalDoGenerate(JvmModelGenerator.java:1756)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.doGenerate(JvmModelGenerator.java:181)
Re: NullPointerException while invoking JvmModelInferrer/Generator for code generation from a Java P [message #1252664 is a reply to message #1252636] Fri, 21 February 2014 13:09 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13641
Registered: July 2009
Senior Member
Never ever call a standalone setup if you are not standalone !!!

http://koehnlein.blogspot.de/2012/11/xtext-tip-how-do-i-get-guice-injec
tor.html?m=1

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


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 while invoking JvmModelInferrer/Generator for code generation from a Java P [message #1252916 is a reply to message #1252664] Fri, 21 February 2014 18:52 Go to previous messageGo to next message
Priya Sabut is currently offline Priya SabutFriend
Messages: 41
Registered: September 2012
Member
Hi,
The above mentioned link is not working. Could you please tell what am I missing here ?

Thank you...
Re: NullPointerException while invoking JvmModelInferrer/Generator for code generation from a Java P [message #1252969 is a reply to message #1252916] Fri, 21 February 2014 20:11 Go to previous message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13641
Registered: July 2009
Senior Member
argg http://koehnlein.blogspot.de/2012/11/xtext-tip-how-do-i-get-guice-injector.html?m=1

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:Xtext adding new DSL syntax dynamically
Next Topic:Problem related to scope provider
Goto Forum:
  


Current Time: Wed Oct 28 14:22:20 GMT 2020

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

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

Back to the top