Blazor-Diagrams / Blazor.Diagrams

A fully customizable and extensible all-purpose diagrams library for Blazor
https://blazor-diagrams.zhaytam.com
MIT License
919 stars 176 forks source link

Nullreference exception when adding link model target/source ports #263

Closed AlmightyLks closed 1 year ago

AlmightyLks commented 1 year ago

For some reason the following code throws a NRE Am I doing something wrong?

            Diagram.Nodes.Add(current);

            if (parent is not null)
            {
                var link = new LinkModel(parent, current)
                {
                    TargetMarker = LinkMarker.Arrow
                };
                link.SetSourcePort(parent.GetPort(PortAlignment.Bottom));
                link.SetTargetPort(current.GetPort(PortAlignment.Top));
                Diagram.Links.Add(link);
            }

image

Stacktrace:

   at Blazor.Diagrams.Core.Layers.LinkLayer.OnItemAdded(BaseLinkModel link)
   at Blazor.Diagrams.Core.BaseLayer`1.Add(T item)
   at ForkHierarchy.Pages.Index.<RenderNodeAsync>d__24.MoveNext()

Checking whether I am adding a null value to SetSource/TargetPort, shows me that they do have values: image

AlmightyLks commented 1 year ago

Oh and the reason why I suspect the source / target port setters is that it doesnt occur when not setting them: image

AlmightyLks commented 1 year ago

Digging into this: image

zHaytam commented 1 year ago

Hello, what version are you using?

AlmightyLks commented 1 year ago

Latest stable release, 2.1.6

Debugging the current master branch source to investigate

AlmightyLks commented 1 year ago

image When Node#GetPort(), shouldn't the returning Port's parent be the Node I used it on? ^^

zHaytam commented 1 year ago

Is there a possibility SourceNode is null? Even though it seems impossible, since its being set in https://github.com/Blazor-Diagrams/Blazor.Diagrams/blob/master/src/Blazor.Diagrams.Core/Models/Base/BaseLinkModel.cs#L70

zHaytam commented 1 year ago

image When Node#GetPort(), shouldn't the returning Port's parent be the Node I used it on? ^^

Interesting. Can you show me how are you creating these nodes and ports?

AlmightyLks commented 1 year ago

image

I think this is the only place where I create both, the node models and the ports

zHaytam commented 1 year ago

The type of SourcePort is NodePort? This is very wierd... The only way Parent could be null is if you pass null to it. Cna you show me the NodePort class?

AlmightyLks commented 1 year ago

image Oh, funny thing. I had this in my portmodel

Is a Port's Parent the node's parent or is a Port's Parent the node itself?

zHaytam commented 1 year ago

A port's parent is the node itself. A node doesn't have a parent (at least in the library, so that might be your own property?)

AlmightyLks commented 1 year ago

Would make sense, yeah 😄

AlmightyLks commented 1 year ago

One more question related to this feature image It isn't showing any links at all now 😄

Am I using it right?

zHaytam commented 1 year ago

I'll need more information. Firstly, how are you rendering your ports?

AlmightyLks commented 1 year ago

Oh! I didn't know you had to add a PortRenderer to the custom node razor file! 😄 My bad

With this all my questions in this regards were answered. Thanks