Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » NPE when serializing an in-memory model
NPE when serializing an in-memory model [message #1122115] Tue, 01 October 2013 08:42 Go to next message
Lars Martin is currently offline Lars MartinFriend
Messages: 24
Registered: July 2009
Junior Member
I try to serialize an in-memory model thus no valid underlaying resource and URI exist.

java.lang.NullPointerException
	at org.eclipse.xtext.ui.editor.formatting.PreferenceStoreWhitespaceInformationProvider.getLineSeparatorPreference(PreferenceStoreWhitespaceInformationProvider.java:56)
	at org.eclipse.xtext.ui.editor.formatting.PreferenceStoreWhitespaceInformationProvider.getLineSeparatorInformation(PreferenceStoreWhitespaceInformationProvider.java:46)
	at org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter.getLineSeparatorInfo(AbstractDeclarativeFormatter.java:114)
	at org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter.createFormattingConfig(AbstractDeclarativeFormatter.java:81)
	at org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter.getConfig(AbstractDeclarativeFormatter.java:92)
	at org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter.createFormatterStream(AbstractDeclarativeFormatter.java:75)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:103)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:122)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:51)


The model is generated on the fly:
IParseResult parseResult = parser.parse(new StringReader("..."));
Model element = parseResult.getRootASTElement();
...
serializer.serialize(element);


Is this supposed to work?

Regards, Lars M.
Re: NPE when serializing an in-memory model [message #1122143 is a reply to message #1122115] Tue, 01 October 2013 09:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6550
Registered: July 2009
Senior Member
Hi,

i guess you need at least a fake resource with a fake uri. (at least when you run this code from ui)
i am not sure if this is to be considered as a bug or not.

e.g.

ResourceSet rs = injector.getInstance(ResourceSet.class);
		Resource r = rs.createResource(URI.createURI("fake.mydsl"));
		r.getContents().add(model);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		r.save(baos, SaveOptions.defaultOptions().toOptionsMap());
		String content = baos.toString("UTF-8");


or you bind a more nullsafe PreferenceStoreWhitespaceInformationProvider
Re: NPE when serializing an in-memory model [message #1122166 is a reply to message #1122143] Tue, 01 October 2013 09:35 Go to previous messageGo to next message
Moritz Eysholdt is currently offline Moritz EysholdtFriend
Messages: 143
Registered: July 2009
Location: Kiel, Germany
Senior Member
A model is always expected to have a resource. And as Christian suggested, the URI doesn't need to represent an actual file in the file system. It's a Unified Resource Identifier after all and not a Unified Resource Locator (URI vs. URL) Smile Furthermore, a resource always needs to be in a ResourceSet.
Re: NPE when serializing an in-memory model [message #1122287 is a reply to message #1122166] Tue, 01 October 2013 12:05 Go to previous message
Lars Martin is currently offline Lars MartinFriend
Messages: 24
Registered: July 2009
Junior Member
Thanks guys. Works as expected.
Previous Topic:DSL file encoding
Next Topic:Model to Text error: resolution of uriFragment 'xtextLink ...
Goto Forum:
  


Current Time: Mon Dec 22 04:58:10 GMT 2014

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

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