Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to fix the DanglingHREFException
How to fix the DanglingHREFException [message #422090] Thu, 21 August 2008 17:01 Go to next message
Li Junhong is currently offline Li JunhongFriend
Messages: 1012
Registered: July 2009
Senior Member
Hi all,

I'm a newer to EMF, I found that many posts discussed about
DanglingHREFException in the newsgroup. I know that all the objects needed
to be serialized must be contained in a resource. But here is my case.

For example I have a model named Gate, it contains two references named
sourcegate and repeatgates(a list), they're in eOpposite relationship and
non-containment. It means if I set the Gate A as the repeat gate of Gate
B, EMF will set B as the source gate of A automatically.

Now, I need to reference the gate cross two model files. Here is my code,
sourceGate and repeatGate are existed in two different files.
////////////////////////////////////////////
ResourceSet rscSet = sourceGate.eResource().getResourceSet();
URI fileURI = URI.createFileURI("D:/Model/repeatGate.model");
Resource repeatGateRsc = rscSet.getResource(fileURI, true);

Gate repeatGate = (Gate) repeatGateRsc.getContents().get(0);
// repeateGate.eResource != null
assert(repeatGate.eResource() != null);
sourceGate.getRepeatGates().add(repeatGate);

sourceGate.eResource.save(saveOptions);
///////////////////////////////////////////

It pops up the DanglingHREFException and says that the repeatGate is not
in a resource.

Can someone help me to solve this problem? Any pointers would be
appreciated, thanks!~

Regards!
Li Junhong
Re: How to fix the DanglingHREFException [message #422091 is a reply to message #422090] Thu, 21 August 2008 17:18 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------070007020308080706070704
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Li,

Comments below.

Li Junhong wrote:
> Hi all,
>
> I'm a newer to EMF, I found that many posts discussed about
> DanglingHREFException in the newsgroup. I know that all the objects
> needed to be serialized must be contained in a resource. But here is
> my case.
You're hoping to be an exception to the rule? :-P
>
> For example I have a model named Gate, it contains two references
> named sourcegate and repeatgates(a list), they're in eOpposite
> relationship and non-containment. It means if I set the Gate A as the
> repeat gate of Gate B, EMF will set B as the source gate of A
> automatically.
Like the relation between Writer and Book in the Library model...
>
> Now, I need to reference the gate cross two model files. Here is my
> code, sourceGate and repeatGate are existed in two different files.
> ////////////////////////////////////////////
> ResourceSet rscSet = sourceGate.eResource().getResourceSet();
> URI fileURI = URI.createFileURI("D:/Model/repeatGate.model");
> Resource repeatGateRsc = rscSet.getResource(fileURI, true);
>
> Gate repeatGate = (Gate) repeatGateRsc.getContents().get(0);
> // repeateGate.eResource != null
> assert(repeatGate.eResource() != null);
> sourceGate.getRepeatGates().add(repeatGate);
What happens if you add this line here?

assert(repeatGate.eResource() != null);

I can't imagine it becoming null as a result.
> sourceGate.eResource.save(saveOptions);
> ///////////////////////////////////////////
I suspect that whatever the problem is will be in lines you haven't shown...

If you put a breakpoint on the constructor for the exception, you should
be able to see which object has the problem...
>
> It pops up the DanglingHREFException and says that the repeatGate is
> not in a resource.
>
> Can someone help me to solve this problem? Any pointers would be
> appreciated, thanks!~
>
> Regards!
> Li Junhong
>

--------------070007020308080706070704
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Li,<br>
<br>
Comments below.<br>
<br>
Li Junhong wrote:
<blockquote
cite="mid:36c7f20805ecf5fde9242e83ae976864$1@www.eclipse.org"
type="cite">Hi all,
<br>
<br>
I'm a newer to EMF, I found that many posts discussed about
DanglingHREFException in the newsgroup. I know that all the objects
needed to be serialized must be contained in a resource. But here is my
case.
<br>
</blockquote>
You're hoping to be an exception to the rule? :-P<br>
<blockquote
cite="mid:36c7f20805ecf5fde9242e83ae976864$1@www.eclipse.org"
type="cite"><br>
For example I have a model named Gate, it contains two references named
sourcegate and repeatgates(a list), they're in eOpposite relationship
and non-containment. It means if I set the Gate A as the repeat gate of
Gate B, EMF will set B as the source gate of A automatically.
<br>
</blockquote>
Like the relation between Writer and Book in the Library model...<br>
<blockquote
cite="mid:36c7f20805ecf5fde9242e83ae976864$1@www.eclipse.org"
type="cite"><br>
Now, I need to reference the gate cross two model files. Here is my
code, sourceGate and repeatGate are existed in two different files.
<br>
////////////////////////////////////////////
<br>
ResourceSet rscSet = sourceGate.eResource().getResourceSet();
<br>
URI fileURI = URI.createFileURI("D:/Model/repeatGate.model");
<br>
Resource repeatGateRsc = rscSet.getResource(fileURI, true);
<br>
<br>
Gate repeatGate = (Gate) repeatGateRsc.getContents().get(0);
<br>
// repeateGate.eResource != null
<br>
assert(repeatGate.eResource() != null);
<br>
sourceGate.getRepeatGates().add(repeatGate);
<br>
</blockquote>
What happens if you add this line here?<br>
<blockquote>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to fix the DanglingHREFException [message #422101 is a reply to message #422091] Fri, 22 August 2008 01:42 Go to previous messageGo to next message
Li Junhong is currently offline Li JunhongFriend
Messages: 1012
Registered: July 2009
Senior Member
Hi Ed,

Actually you're right, the relation of my model is just the same the one
between Writer and Book in the Library model.

assert(repeatGate.eResource() != null);
assert(sourceGate.eResource() != null);

Of coz, the above two assertions are correct, the resource of two gates
aren't null.

But it still pops up the exception says that repeatGate is not in a
resource. It's weird. Could you give me some more suggestion? Thanks for
your kindly help!
Re: How to fix the DanglingHREFException [message #422107 is a reply to message #422091] Fri, 22 August 2008 06:17 Go to previous messageGo to next message
Li Junhong is currently offline Li JunhongFriend
Messages: 1012
Registered: July 2009
Senior Member
Hi Ed,

Following your guide, I set a break point at the constructor of
DanglingHREFException.

I found that the reason for it is that in the process of saving the
sourceGate's resource, when it try to call saveHref(EObject remote,
EStructuralFeature f), it works fine if the remote argument existed in the
sourceGate's resource. However, when it is trying to save the reference of
repeatGate which comes from another model file(another resource), in the
method getURIFragment(EObject eObject) of class ResourceImpl, as all of
the eDirectResource() of repeatGate do not equal the resource of
sourceGate, it returns "/-1" for the URIFragment. That's the key why it
always pops up DanglingHREFException.

My question is that how can I make the reference of my repeatGate
contained in the resource of sourceGate? Does my code miss something
important? It's better that you can show me some piece of codes to save
the reference correctly.

Sorry for the naive question comes from this EMF newer. Thanks in advance.
Re: How to fix the DanglingHREFException [message #422114 is a reply to message #422107] Fri, 22 August 2008 09:52 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Li,

Comments below.


Li Junhong wrote:
> Hi Ed,
>
> Following your guide, I set a break point at the constructor of
> DanglingHREFException.
> I found that the reason for it is that in the process of saving the
> sourceGate's resource, when it try to call saveHref(EObject remote,
> EStructuralFeature f), it works fine if the remote argument existed in
> the sourceGate's resource. However, when it is trying to save the
> reference of repeatGate which comes from another model file(another
> resource), in the method getURIFragment(EObject eObject) of class
> ResourceImpl, as all of the eDirectResource() of repeatGate do not
> equal the resource of sourceGate, it returns "/-1" for the
> URIFragment. That's the key why it always pops up DanglingHREFException.
>
> My question is that how can I make the reference of my repeatGate
> contained in the resource of sourceGate? Does my code miss something
> important? It's better that you can show me some piece of codes to
> save the reference correctly.
For the object it's actually complaining about, does eResource() for it
return a non-null result? Here's another thing to try. Using
resource.getAllContents() on the resource you're about to serialize.
For each EObject, use iterator over the eCrossReferences(). Check each
cross referenced EObject to see if eResource() == null. If you find
such an object, that's a problem. That object needs to be added to some
resource.
>
> Sorry for the naive question comes from this EMF newer. Thanks in
> advance.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Get container feature of an EClass
Next Topic:Generics for multiplicity-many references
Goto Forum:
  


Current Time: Fri Apr 26 13:49:25 GMT 2024

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

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

Back to the top