Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Persisting object as XMI using Ecore model(Create a XMI file using Ecore model with object values)
Persisting object as XMI using Ecore model [message #1233182] Sat, 18 January 2014 22:15 Go to next message
Siddhartha Moitra is currently offline Siddhartha MoitraFriend
Messages: 2
Registered: January 2014
Junior Member
Hello,

I am new to EMF and I have created a simple model and generated Java code from it. Now I need to persist the object values that I set using the technique.

Here is my model code:
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version=.....
    name="library" nsURI="http:// library/1.0" nsPrefix="library">
  <eClassifiers xsi:type="ecore:EClass" name="Book">
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="title" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="pages" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
    <eStructuralFeatures xsi:type="ecore:EReference" name="author" lowerBound="1"
        eType="#//Writer"/>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="Writer">
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
  </eClassifiers>
</ecore:EPackage>


And the Java code is generated well. However when I execute the code:
// Initialize the model
		LibraryPackage.eINSTANCE.eClass();

		// Retrieve the default factory singleton
		factory = new LibraryFactoryImpl();

		// Create the content of the model via this program
		Book book = factory.createBook();
		book.setTitle("Title of Book");
		book.setPages(200);
		
		Writer author = factory.createWriter();
		author.setName("Siddhartha");
		
		book.setAuthor(author);

		// Obtain a new resource set
		ResourceSet resSet = new ResourceSetImpl();

		// Register the default resource factory -- only needed for stand-alone!
		resSet.getResourceFactoryRegistry()
				.getExtensionToFactoryMap()
				.put(Resource.Factory.Registry.DEFAULT_EXTENSION,
						new XMIResourceFactoryImpl());

		URI fileURI = URI.createFileURI(new File("library.xml")
				.getAbsolutePath());

		// create a resource
		Resource resource = resSet.createResource(fileURI);
		// Get the first model element and cast it to the right type, in my
		// example everything is hierarchical included in this first node
		resource.getContents().add(book);
		// resource.getContents().add(webPage);

		// now save the content.
		try {
			resource.save(Collections.EMPTY_MAP);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


I get an exception: java.lang.NullPointerException
at org.eclipse.emf.ecore.EcorePackage$Literals.<clinit>(EcorePackage.java:5145)
... 20 more

The reason as shown by debugger is, in EcorePackage$Literals:
EClass EATTRIBUTE = eINSTANCE.getEAttribute();

Here the eINSTANCE is NULL.

Could you please help me with this, if I am doing anything wrong.

Thanks in advance.
Re: Persisting object as XMI using Ecore model [message #1233401 is a reply to message #1233182] Sun, 19 January 2014 14:30 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6537
Registered: July 2009
Senior Member
Hi

Only one line of stack trace is unhelpful.

Adding EcorePackage.eINSTANCE.eClass() to initialize Ecore explicitly
before you use its details might help.

Regards

Ed Willink

On 19/01/2014 01:30, Siddhartha Moitra wrote:
> Hello,
>
> I am new to EMF and I have created a simple model and generated Java
> code from it. Now I need to persist the object values that I set using
> the technique.
>
> Here is my model code:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <ecore:EPackage xmi:version=.....
> name="library" nsURI="http:// library/1.0" nsPrefix="library">
> <eClassifiers xsi:type="ecore:EClass" name="Book">
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="title"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="pages"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
> <eStructuralFeatures xsi:type="ecore:EReference" name="author"
> lowerBound="1"
> eType="#//Writer"/>
> </eClassifiers>
> <eClassifiers xsi:type="ecore:EClass" name="Writer">
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> </eClassifiers>
> </ecore:EPackage>
>
>
> And the Java code is generated well. However when I execute the code:
>
> // Initialize the model
> LibraryPackage.eINSTANCE.eClass();
>
> // Retrieve the default factory singleton
> factory = new LibraryFactoryImpl();
>
> // Create the content of the model via this program
> Book book = factory.createBook();
> book.setTitle("Title of Book");
> book.setPages(200);
>
> Writer author = factory.createWriter();
> author.setName("Siddhartha");
>
> book.setAuthor(author);
>
> // Obtain a new resource set
> ResourceSet resSet = new ResourceSetImpl();
>
> // Register the default resource factory -- only needed for
> stand-alone!
> resSet.getResourceFactoryRegistry()
> .getExtensionToFactoryMap()
> .put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> new XMIResourceFactoryImpl());
>
> URI fileURI = URI.createFileURI(new File("library.xml")
> .getAbsolutePath());
>
> // create a resource
> Resource resource = resSet.createResource(fileURI);
> // Get the first model element and cast it to the right type,
> in my
> // example everything is hierarchical included in this first node
> resource.getContents().add(book);
> // resource.getContents().add(webPage);
>
> // now save the content.
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
>
> I get an exception: java.lang.NullPointerException
> at
> org.eclipse.emf.ecore.EcorePackage$Literals.<clinit>(EcorePackage.java:5145)
> ... 20 more
>
> The reason as shown by debugger is, in EcorePackage$Literals: EClass
> EATTRIBUTE = eINSTANCE.getEAttribute();
>
> Here the eINSTANCE is NULL.
>
> Could you please help me with this, if I am doing anything wrong.
>
> Thanks in advance.
Re: Persisting object as XMI using Ecore model [message #1233407 is a reply to message #1233401] Sun, 19 January 2014 14:56 Go to previous messageGo to next message
Siddhartha Moitra is currently offline Siddhartha MoitraFriend
Messages: 2
Registered: January 2014
Junior Member
Hello,

Here is the complete stacktrace. I added the ecore initializing statement, but no luck.

Exception in thread "main" java.lang.ExceptionInInitializerError
	at org.eclipse.emf.ecore.impl.EPackageImpl.eStaticClass(EPackageImpl.java:236)
	at org.eclipse.emf.ecore.impl.MinimalEObjectImpl.eClass(MinimalEObjectImpl.java:688)
	at org.eclipse.emf.ecore.impl.ENamedElementImpl.eContents(ENamedElementImpl.java:224)
	at org.eclipse.emf.ecore.util.EcoreUtil$ProperContentIterator.<init>(EcoreUtil.java:1290)
	at org.eclipse.emf.ecore.util.EcoreUtil$4.getChildren(EcoreUtil.java:1201)
	at org.eclipse.emf.ecore.util.EcoreUtil$ContentTreeIterator.hasNext(EcoreUtil.java:1452)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.attached(ResourceImpl.java:844)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl$ContentsEList.inverseAdd(ResourceImpl.java:411)
	at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUnique(NotifyingListImpl.java:312)
	at org.eclipse.emf.common.util.AbstractEList.add(AbstractEList.java:301)
	at org.eclipse.emf.ecore.impl.EPackageImpl.createResource(EPackageImpl.java:753)
	at org.eclipse.emf.ecore.impl.EcorePackageImpl.initializePackageContents(EcorePackageImpl.java:2838)
	at org.eclipse.emf.ecore.impl.EcorePackageImpl.init(EcorePackageImpl.java:485)
	at org.eclipse.emf.ecore.EcorePackage.<clinit>(EcorePackage.java:67)
	at de.sid.emf.webpage.SaveModelObject.saveModelObject(SaveModelObject.java:34)
	at de.sid.emf.webpage.main.SaveLoadModelObjectMain.main(SaveLoadModelObjectMain.java:19)
Caused by: java.lang.NullPointerException
	at org.eclipse.emf.ecore.EcorePackage$Literals.<clinit>(EcorePackage.java:5145)
	... 16 more
Re: Persisting object as XMI using Ecore model [message #1233451 is a reply to message #1233407] Sun, 19 January 2014 17:35 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6537
Registered: July 2009
Senior Member
Hi

It is not clear to me how saveModelObject(SaveModelObject.java:34)
relates to your earlier message.

You might find that following
http://wiki.eclipse.org/OCL/ForumNetiquette reduces the need to play
ping pong.

Regards

Ed Willink


On 19/01/2014 14:56, Siddhartha Moitra wrote:
> Hello,
>
> Here is the complete stacktrace. I added the ecore initializing
> statement, but no luck.
>
>
> Exception in thread "main" java.lang.ExceptionInInitializerError
> at
> org.eclipse.emf.ecore.impl.EPackageImpl.eStaticClass(EPackageImpl.java:236)
> at
> org.eclipse.emf.ecore.impl.MinimalEObjectImpl.eClass(MinimalEObjectImpl.java:688)
> at
> org.eclipse.emf.ecore.impl.ENamedElementImpl.eContents(ENamedElementImpl.java:224)
> at
> org.eclipse.emf.ecore.util.EcoreUtil$ProperContentIterator.<init>(EcoreUtil.java:1290)
> at
> org.eclipse.emf.ecore.util.EcoreUtil$4.getChildren(EcoreUtil.java:1201)
> at
> org.eclipse.emf.ecore.util.EcoreUtil$ContentTreeIterator.hasNext(EcoreUtil.java:1452)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.attached(ResourceImpl.java:844)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl$ContentsEList.inverseAdd(ResourceImpl.java:411)
> at
> org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUnique(NotifyingListImpl.java:312)
> at
> org.eclipse.emf.common.util.AbstractEList.add(AbstractEList.java:301)
> at
> org.eclipse.emf.ecore.impl.EPackageImpl.createResource(EPackageImpl.java:753)
> at
> org.eclipse.emf.ecore.impl.EcorePackageImpl.initializePackageContents(EcorePackageImpl.java:2838)
> at
> org.eclipse.emf.ecore.impl.EcorePackageImpl.init(EcorePackageImpl.java:485)
> at org.eclipse.emf.ecore.EcorePackage.<clinit>(EcorePackage.java:67)
> at
> de.sid.emf.webpage.SaveModelObject.saveModelObject(SaveModelObject.java:34)
> at
> de.sid.emf.webpage.main.SaveLoadModelObjectMain.main(SaveLoadModelObjectMain.java:19)
> Caused by: java.lang.NullPointerException
> at
> org.eclipse.emf.ecore.EcorePackage$Literals.<clinit>(EcorePackage.java:5145)
> ... 16 more
>
Previous Topic:temporarily disable notifications
Next Topic:Getting from an EPackage to the EPackage.Registry it was registered with
Goto Forum:
  


Current Time: Wed Dec 11 00:36:22 GMT 2019

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

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

Back to the top