Blazor-Diagrams / Blazor.Diagrams

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

Exception when copying node #400

Closed mdouble closed 6 months ago

mdouble commented 6 months ago

When I try to copy a node I get a System.NullReferenceException: "Object reference not set to an instance of an object." from

public static Task SendAsync(this IClientProxy clientProxy, string method, object? arg1, object? arg2, object? arg3, CancellationToken cancellationToken = default(CancellationToken))
{
    return clientProxy.SendCoreAsync(method, new object[3] { arg1, arg2, arg3 }, cancellationToken);
}

in Microsoft.AspNetCore.SignalR.ClientProxyExtensions.

Here is my code:

Point newPosition = new Point(Position.X, Position.Y + 50);
SimpleTaskNode node = _parent.CreateNewNodeOnPosition(newPosition);                    
foreach (LinkModel linkModel in _parent.Diagram.Links)
{
      var sourceNode = (linkModel.Source as SinglePortAnchor)!.Port.Parent;
      var targetNode = (linkModel.Target as SinglePortAnchor)!.Port.Parent;
      if (sourceNode == this)
      {
           LinkModel link = new LinkModel(node.GetPort(PortAlignment.Right), targetNode.GetPort(PortAlignment.Left));
           _parent.Diagram.Links.Add(link);
       }
       if (targetNode == this)
       {
            LinkModel link = new LinkModel(sourceNode.GetPort(PortAlignment.Right), node.GetPort(PortAlignment.Left));
             _parent.Diagram.Links.Add(link);
        }                         
}

The exception is thrown when I add a link.

mdouble commented 6 months ago

Stupid mistake on my side. I change the collection while I loop through it.