I'm working on a simple version of a BPMN 2.0 editor based on the EMF model of the BPMN2 subproject of MDT. In this EMF model you have for example a StartEvent and EndEvent that are eventually EObjects. I'm able to use the Graphiti tutorial example code together with these Start and End Event objects and that looks great.
But now I run into an issue with connecting a StartEvent with an EndEvent. According to the BPMN2 EMF model I should use a SequenceFlow, with is eventually also a EObject. Now I'm trying to create a Connection between a Start and End Event with a SequenceFlow. Everything goes well until the link method of the AbstractFeature class is invoked from the public PictogramElement add(IAddContext context) method in the TutorialCreateEReferenceFeature equivalent class. There I get a NullPointer on the following line (600) of the AbstractFeatureProvider class:
In the tutorial a EReference instance is used to implement the Connection, and that's working okay ofcourse.
But can I also use a EMF object that extends EObject to implement such a Connection? Am I doing something wrong here?
I've worked a bit further with this and I just removed the link method call from my public PictogramElement add(IAddContext context) method and it works great.
I've looked at the code behind this link method, but I can't really figure out what I'm missing by not invoking it ;). Maybe somebody can help me with understanding this?
the problem with the null pointer is that the
EObject you want to link to the connection
is not contained in an EResource or the resource
of the EObject is not part of a ResourceSet.
Before linking your EObject you have to make sure that
1) it is contained by a resource
2) the resource is managed by a resource set
(you may use the resource set of the diagram, accessible via
diagram.eResource.getResourceSet())
The same issue would occur with shapes, that is, the issue is not specific for connections.
Best, Tim
ps: If you do not call the link method, a delete of the connection would
not delete the EObject you are visualizing, so i think this is not a good idea. This explains also why your EObject has to be contained in an EResource: the framework needs to know how it is serialized to be able to delete it.
Thanks!
Now that you explained this I looked back at my code and saw that I already added my SequenceFlow object to the ResourceSet of the diagram with the following line: