Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » createResource() returns null, what am I missing?
createResource() returns null, what am I missing? [message #1020238] Sun, 17 March 2013 17:19 Go to next message
Nigel Daniels is currently offline Nigel Daniels
Messages: 66
Registered: July 2009
Member
Hi,

I'm trying to use EMF in a servlet so I'm having to create a lot of the
default environment by hand. To start up my model I'm trying to do the
following:

FlowchartPackage flowchartPackage = FlowchartPackage.eINSTANCE;
FlowchartFactory flowchartFactory = FlowchartFactory.eINSTANCE;

// Create a resource set and register the package and resource factory
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getPackageRegistry().put(flowchartPackage.getNsURI(),
flowchartPackage);
resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(fileExtension,
new XMIResourceFactoryImpl());

// Generate the root object
FlowDiagram flowDiagram = flowchartFactory.createFlowDiagram();
EcoreUtil.setID(flowDiagram, EcoreUtil.generateUUID());

// Create the file uri & resource to save
File file = new File("test." + fileExtension);
URI fileURI = URI.createURI(file.getAbsolutePath());

/////// This returns null?
Resource resource = resourceSet.createResource(fileURI);
/////// This causes a null pointer exception as a result
resource.getContents().add(flowDiagram);

I thought that adding the package and the XMI resource factory by hand
was all I required to create resouces but I must be missing something.
Any suggestions on what I need to do?

Thanks for any help, Nigel.
Re: createResource() returns null, what am I missing? [message #1020243 is a reply to message #1020238] Sun, 17 March 2013 17:30 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4098
Registered: July 2009
Senior Member
Hi

You may get on better using the extension factory map for extensions.

Regards

Ed Willink

On 17/03/2013 17:19, Nigel Daniels wrote:
> Hi,
>
> I'm trying to use EMF in a servlet so I'm having to create a lot of
> the default environment by hand. To start up my model I'm trying to do
> the following:
>
> FlowchartPackage flowchartPackage = FlowchartPackage.eINSTANCE;
> FlowchartFactory flowchartFactory = FlowchartFactory.eINSTANCE;
>
> // Create a resource set and register the package and resource factory
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getPackageRegistry().put(flowchartPackage.getNsURI(),
> flowchartPackage);
> resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(fileExtension,
> new XMIResourceFactoryImpl());
>
> // Generate the root object
> FlowDiagram flowDiagram = flowchartFactory.createFlowDiagram();
> EcoreUtil.setID(flowDiagram, EcoreUtil.generateUUID());
>
> // Create the file uri & resource to save
> File file = new File("test." + fileExtension);
> URI fileURI = URI.createURI(file.getAbsolutePath());
>
> /////// This returns null?
> Resource resource = resourceSet.createResource(fileURI);
> /////// This causes a null pointer exception as a result
> resource.getContents().add(flowDiagram);
>
> I thought that adding the package and the XMI resource factory by hand
> was all I required to create resouces but I must be missing something.
> Any suggestions on what I need to do?
>
> Thanks for any help, Nigel.
Re: createResource() returns null, what am I missing? [message #1020275 is a reply to message #1020243] Sun, 17 March 2013 19:52 Go to previous messageGo to next message
Nigel Daniels is currently offline Nigel Daniels
Messages: 66
Registered: July 2009
Member
Hi Ed,

thanks for that, it's now working. I appreciate the help.

Nigel

On 17/03/2013 17:30, Ed Willink wrote:
> Hi
>
> You may get on better using the extension factory map for extensions.
>
> Regards
>
> Ed Willink
>
> On 17/03/2013 17:19, Nigel Daniels wrote:
>> Hi,
>>
>> I'm trying to use EMF in a servlet so I'm having to create a lot of
>> the default environment by hand. To start up my model I'm trying to do
>> the following:
>>
>> FlowchartPackage flowchartPackage = FlowchartPackage.eINSTANCE;
>> FlowchartFactory flowchartFactory = FlowchartFactory.eINSTANCE;
>>
>> // Create a resource set and register the package and resource factory
>> ResourceSet resourceSet = new ResourceSetImpl();
>> resourceSet.getPackageRegistry().put(flowchartPackage.getNsURI(),
>> flowchartPackage);
>> resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(fileExtension,
>> new XMIResourceFactoryImpl());
>>
>> // Generate the root object
>> FlowDiagram flowDiagram = flowchartFactory.createFlowDiagram();
>> EcoreUtil.setID(flowDiagram, EcoreUtil.generateUUID());
>>
>> // Create the file uri & resource to save
>> File file = new File("test." + fileExtension);
>> URI fileURI = URI.createURI(file.getAbsolutePath());
>>
>> /////// This returns null?
>> Resource resource = resourceSet.createResource(fileURI);
>> /////// This causes a null pointer exception as a result
>> resource.getContents().add(flowDiagram);
>>
>> I thought that adding the package and the XMI resource factory by hand
>> was all I required to create resouces but I must be missing something.
>> Any suggestions on what I need to do?
>>
>> Thanks for any help, Nigel.
>
Re: createResource() returns null, what am I missing? [message #1020281 is a reply to message #1020243] Sun, 17 March 2013 20:15 Go to previous message
Nigel Daniels is currently offline Nigel Daniels
Messages: 66
Registered: July 2009
Member
Hi Ed,

thanks for that, it's now working. I appreciate the help.

Nigel

On 17/03/2013 17:30, Ed Willink wrote:
> Hi
>
> You may get on better using the extension factory map for extensions.
>
> Regards
>
> Ed Willink
>
> On 17/03/2013 17:19, Nigel Daniels wrote:
>> Hi,
>>
>> I'm trying to use EMF in a servlet so I'm having to create a lot of
>> the default environment by hand. To start up my model I'm trying to do
>> the following:
>>
>> FlowchartPackage flowchartPackage = FlowchartPackage.eINSTANCE;
>> FlowchartFactory flowchartFactory = FlowchartFactory.eINSTANCE;
>>
>> // Create a resource set and register the package and resource factory
>> ResourceSet resourceSet = new ResourceSetImpl();
>> resourceSet.getPackageRegistry().put(flowchartPackage.getNsURI(),
>> flowchartPackage);
>> resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put(fileExtension,
>> new XMIResourceFactoryImpl());
>>
>> // Generate the root object
>> FlowDiagram flowDiagram = flowchartFactory.createFlowDiagram();
>> EcoreUtil.setID(flowDiagram, EcoreUtil.generateUUID());
>>
>> // Create the file uri & resource to save
>> File file = new File("test." + fileExtension);
>> URI fileURI = URI.createURI(file.getAbsolutePath());
>>
>> /////// This returns null?
>> Resource resource = resourceSet.createResource(fileURI);
>> /////// This causes a null pointer exception as a result
>> resource.getContents().add(flowDiagram);
>>
>> I thought that adding the package and the XMI resource factory by hand
>> was all I required to create resouces but I must be missing something.
>> Any suggestions on what I need to do?
>>
>> Thanks for any help, Nigel.
>
Previous Topic:How to control indent in the progress of resource serialization?
Next Topic:EMF Databinding with double values
Goto Forum:
  


Current Time: Sat Oct 25 19:52:32 GMT 2014

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

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