Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) NullReferenceException when setting DiagramItem.Selected = false (Read 6150 times)
Kris
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: May 17th, 2017
NullReferenceException when setting DiagramItem.Selected = false
Jul 4th, 2017 at 1:32pm
Print Post  
Hi,

In some cases, when I set the Selected property of DiagramItem to false I got the NullReferenceException at: MindFusion.Diagramming.Wpf.Selection.RemoveItem(UIElement item)

any idea what can be null when the item is being removed from selection which cause the nullreference exc ?

Regards
Kris
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #1 - Jul 4th, 2017 at 2:16pm
Print Post  
Hi,

Is Diagram.ActiveItem null before you change Selected?

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Kris
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: May 17th, 2017
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #2 - Jul 4th, 2017 at 2:46pm
Print Post  
Hi,

no, it is not null, I've just debugged it: before setting Selected = false, ActiveItem is not null,
after setting, the exception is thrown and in this moment the ActiveItem is null

Regards,
Kris
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #3 - Jul 4th, 2017 at 3:38pm
Print Post  
Does the SelectionChanged event get raised before exception is thrown?
  
Back to top
 
IP Logged
 
Kris
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: May 17th, 2017
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #4 - Jul 6th, 2017 at 8:50am
Print Post  
Looks like yes.
The same exception I got when removing node from the Diagram - exc is thrown in the same place.
The case with removing node is quite random.
Have no idea what can be wrong ...

Regards,

  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #5 - Jul 6th, 2017 at 9:26am
Print Post  
I can see RaiseSelectionChanged called on last line of Selection.RemoveItem method. I guess if the event handler is called successfully, then the exception either happens inside it or is somehow caused by how it interacts with the diagram. What does the handler code look like and what is the exception's full call-stack?

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Kris
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: May 17th, 2017
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #6 - Jul 6th, 2017 at 9:50am
Print Post  
Hi,

for the node removal case:
   at MindFusion.Diagramming.Wpf.Selection.RemoveItem(UIElement item)
   at MindFusion.Diagramming.Wpf.Diagram.LI(DiagramItem C)
   at MindFusion.Diagramming.Wpf.Commands.RemoveItemCmd.Execute(Boolean undoEnabled)
   at MindFusion.Diagramming.Wpf.UndoManager.V(Command C)
   at MindFusion.Diagramming.Wpf.Diagram.DL(DiagramItem C)
   at MindFusion.Diagramming.Wpf.Diagram.OnRemovingItem(Object sender, ItemEventArgs e)
   at MindFusion.Diagramming.Wpf.ItemCollectionBase`1.RaiseRemoving(DiagramItem item)
   at MindFusion.Diagramming.Wpf.ItemCollectionBase`1.I(T C, Boolean U)
   at ....CommonDiagram.RemoveNode(INode node)

CommonDiagram inherits from Diagram and Remove node just calls: Nodes.Remove(node)
in this case the selectionchanged event is not raised (at least overriden method OnSelectionChanged is not invoked)

regards,
Kris
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #7 - Jul 6th, 2017 at 2:35pm
Print Post  
Is item.Parent equal to null when the exception's thrown?
  
Back to top
 
IP Logged
 
Kris
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: May 17th, 2017
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #8 - Jul 6th, 2017 at 3:30pm
Print Post  
No, it is not null, it is set to Diagram
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #9 - Jul 10th, 2017 at 6:46am
Print Post  
Are you sure about ActiveItem being non-null then? I can see three lines of code in this method and it's either ActiveItem or Parent. The control supposedly keeps an invariant that ActiveItem is always one of the selected items, but we might be losing it, e.g. if you set ActiveItem from code while processing remove events?
  
Back to top
 
IP Logged
 
Kris
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: May 17th, 2017
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #10 - Jul 11th, 2017 at 2:28pm
Print Post  
Is there any operation on node or diagram (i.e node.Selected = false) to change the ActiveItem indirectly ?

before: Nodes.Remove -> ActiveItem is not null
after exc is thrown -> ActiveItem is null, Parent is not null

We do not change it directly in the code, so maybe it is changed indirectly, but still have no clue where ...
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #11 - Jul 11th, 2017 at 5:20pm
Print Post  
Selection.RemoveItem sets ActiveItem to either another item from selection, or null if there aren't any selected items left. If there's a single item selected, RemoveItem assumes that's the ActiveItem. There might be some combination of selection changes or deletions from deleted / selected events that breaks that invariant somehow. We'll add null checks there to make sure.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #12 - Jul 12th, 2017 at 8:17am
Print Post  
Please check if this build fixes it -
https://mindfusion.eu/_beta/wpfdiag35.zip

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Kris
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: May 17th, 2017
Re: NullReferenceException when setting DiagramItem.Selected = false
Reply #13 - Jul 13th, 2017 at 12:14pm
Print Post  
Thx,

I worked whole day on this build, looks like solved the problems (and other cases related to this nullrefexc).

Thx
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint