Skip to content

Commit 8065166

Browse files
Copilotdwarwick
andcommitted
Fix null reference error when clearing diagram before rebuild
Co-authored-by: dwarwick <15970276+dwarwick@users.noreply.github.com>
1 parent 173293f commit 8065166

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

JwtIdentity.Client/Pages/Survey/BranchingSurveyEdit.razor.cs

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -433,39 +433,52 @@ protected async Task UpdateTrueFalseBranch(TrueFalseQuestionViewModel question,
433433

434434
protected async Task RefreshDiagram()
435435
{
436-
if (diagram != null)
436+
// Clear the existing collections that are bound to the diagram
437+
Nodes.Clear();
438+
Connectors.Clear();
439+
440+
// Rebuild nodes and connectors into temporary collections
441+
var tempNodes = new DiagramObjectCollection<Node>();
442+
var tempConnectors = new DiagramObjectCollection<Connector>();
443+
444+
// Store the original collections temporarily
445+
var originalNodes = Nodes;
446+
var originalConnectors = Connectors;
447+
448+
// Build into temporary collections
449+
Nodes = tempNodes;
450+
Connectors = tempConnectors;
451+
BuildSyncfusionDiagram();
452+
453+
// Get the built collections
454+
tempNodes = Nodes;
455+
tempConnectors = Connectors;
456+
457+
// Restore original collections and populate them
458+
Nodes = originalNodes;
459+
Connectors = originalConnectors;
460+
461+
// Add all nodes first
462+
foreach (var node in tempNodes)
437463
{
438-
// Clear existing diagram elements
439-
diagram.Clear();
464+
Nodes.Add(node);
440465
}
441-
442-
// Build new nodes and connectors
443-
BuildSyncfusionDiagram();
444-
445-
// Add elements to the diagram using the async method
446-
if (diagram != null && (Nodes.Any() || Connectors.Any()))
466+
467+
// Then add all connectors
468+
foreach (var connector in tempConnectors)
447469
{
448-
// Combine nodes and connectors into a single collection
449-
var diagramElements = new DiagramObjectCollection<NodeBase>();
450-
foreach (var node in Nodes)
451-
{
452-
diagramElements.Add(node);
453-
}
454-
foreach (var connector in Connectors)
455-
{
456-
diagramElements.Add(connector);
457-
}
470+
Connectors.Add(connector);
471+
}
458472

459-
await diagram.AddDiagramElementsAsync(diagramElements);
473+
StateHasChanged();
460474

461-
// Allow UI to update before triggering layout
462-
await Task.Delay(100);
475+
// Allow UI to update before triggering layout
476+
await Task.Delay(100);
463477

464-
// Apply layout
478+
if (diagram != null)
479+
{
465480
await diagram.DoLayoutAsync();
466481
}
467-
468-
StateHasChanged();
469482
}
470483

471484
protected void OnZoomChanged(double newZoom)

0 commit comments

Comments
 (0)