Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Getting a null reference exception when dragging in a node derived from ShapeNode (Read 5193 times)
David Cater
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 12
Joined: May 12th, 2012
Getting a null reference exception when dragging in a node derived from ShapeNode
May 12th, 2012 at 3:31pm
Print Post  
I am trying to create a subclass of ShapeNode and drag that node onto a diagram. When I do that, I get a null reference exception on the drop. Specifically:

PresentationCore.dll!System.Windows.OleServicesContext.OleDoDragDrop(System.Runt
ime.InteropServices.ComTypes.IDataObject dataObject, MS.Win32.UnsafeNativeMethods.IOleDropSource dropSource, int allowedEffects, int[] finalEffect)

My code is very simple.

Code (C++)
Select All
public class ShapeNodeTest : ShapeNode {} 



Code (HTML)
Select All
<Window xmlns:diag="xxxx:// mindfusion.eu/diagramming/wpf"  x:Class="CustomInteractive.MainWindow"
xmlns="xxxx://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="xxxx://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CustomInteractive"
        Title="MainWindow" SizeToContent="WidthAndHeight">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <diag:NodeListView>
            <diag:ShapeNode Shape="Rectangle" Bounds="0,0,30,30" />
            <local:ShapeNodeTest Shape="Rectangle" Bounds="0,0,40,40" />
        </diag:NodeListView>
        <diag:Diagram Grid.Column="1" x:Name="diagram" Width="500" Height="500" AllowDrop="true" />
    </Grid>
</Window>
 



Note the use of ShapeNode and ShapeNodeTest in the NodeListView. If I drag the ShapeNode in, it works fine. If I drag in ShapeNodeTest, I get the exception. Also note that this code works and creates the node successfully in the diagram:

Code (C++)
Select All
void MainWindow_Loaded(object sender, RoutedEventArgs e) {
    var test2 = new ShapeNodeTest { Bounds = new Rect(100, 100, 30, 30), Shape = Shapes.Rectangle };
    diagram.Nodes.Add(test2);
}
 



Is there something I need to know about inheriting from ShapeNode?

Thanks,

David Cater
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Getting a null reference exception when dragging in a node derived from ShapeNode
Reply #1 - May 13th, 2012 at 5:40pm
Print Post  
Hi,

You must implement a copy constructor or override the node's Clone method to enable drag-and-drop from the NodeListView for your class. This is also necessary for clipboard operations, but for them you must also override SaveToXml and LoadFromXml.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Getting a null reference exception when dragging in a node derived from ShapeNode
Reply #2 - Oct 3rd, 2013 at 9:37am
Print Post  
Hi,

I also made a class which inherits from ShapeNode. And I'm getting the same nullreferenceexception when trying to drag and drop it from a nodelistview onto a diagram. In the case of overriding the Clone method, what exactly do I need to add?

Thanks in advance!
Vincent
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Getting a null reference exception when dragging in a node derived from ShapeNode
Reply #3 - Oct 3rd, 2013 at 9:59am
Print Post  
Hi,

Here is a sample derived class that you should be able to both drag-and-drop and copy-paste:

Code
Select All
Diagram.RegisterItemClass(typeof(MyNode), "MyNode", 1);

class MyNode: ShapeNode
{
	public MyNode()
	{
		Priority = 0;
	}

	public MyNode(MyNode prototype) :
		base(prototype)
	{
		Priority = prototype.Priority;
	}

	protected override void SaveToXml(XmlElement xmlElement, XmlPersistContext context)
	{
		base.SaveToXml(xmlElement, context);
		context.WriteInt(Priority, "Priority", xmlElement);
	}

	protected override void LoadFromXml(XmlElement xmlElement, XmlPersistContext context)
	{
		base.LoadFromXml(xmlElement, context);
		Priority = context.ReadInt("Priority", xmlElement);
	}

	public int Priority { get; set; }
} 



The Clone method will automatically call your copy constructor.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Getting a null reference exception when dragging in a node derived from ShapeNode
Reply #4 - Oct 3rd, 2013 at 10:34am
Print Post  
Hi,

I've implemented the example into my class but I'm still getting the null reference exception.

My code looks like this:

Code
Select All
class BR_Shape : ShapeNode
    {
        protected Shape_Type type;
        public string header { get; protected set; }
        public int Priority { get; set; }

        public BR_Shape()
        {
            Priority = 0;
        }

        public BR_Shape(BR_Shape prototype) : base(prototype)
        {
            Priority = prototype.Priority;
        }

        public override DiagramItem Clone(bool clipboard)
        {
            return base.Clone(clipboard);
        }

        protected override void SaveToXml(XmlElement xmlElement, XmlPersistContext context)
        {
            base.SaveToXml(xmlElement, context);
            context.WriteInt(Priority, "Priority", xmlElement);
        }

        protected override void LoadFromXml(XmlElement xmlElement, XmlPersistContext context)
        {
            base.LoadFromXml(xmlElement, context);
            Priority = context.ReadInt("Priority", xmlElement);
        }
    }
 



I also have another class which inherits from NodeListView. In this class I add the shape to the items collection. Do I have to override some methods there as well?

Thanks in advance!
Vincent
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Getting a null reference exception when dragging in a node derived from ShapeNode
Reply #5 - Oct 3rd, 2013 at 10:47am
Print Post  
It is caused by something else then, what's shown at top of the callstack? Try handling the NodeCreated event to see if it's raised; it should occur just at the end of the diagram's OnDrop handler.
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Getting a null reference exception when dragging in a node derived from ShapeNode
Reply #6 - Oct 3rd, 2013 at 11:00am
Print Post  
Hi,

This is the stacktrace of the exception:

Code
Select All
System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=WindowsBase
  StackTrace:
       at MindFusion.Diagramming.Wpf.Diagram.OnDrop(DragEventArgs e)
       at System.Windows.UIElement.OnDropThunk(Object sender, DragEventArgs e)
       at System.Windows.DragEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       at System.Windows.OleDropTarget.RaiseDragEvent(RoutedEvent dragEvent, Int32 dragDropKeyStates, Int32& effects, DependencyObject target, Point targetPoint)
       at System.Windows.OleDropTarget.MS.Win32.UnsafeNativeMethods.IOleDropTarget.OleDrop(Object data, Int32 dragDropKeyStates, Int64 point, Int32& effects)
       at MS.Win32.UnsafeNativeMethods.DoDragDrop(IDataObject dataObject, IOleDropSource dropSource, Int32 allowedEffects, Int32[] finalEffect)
       at System.Windows.OleServicesContext.OleDoDragDrop(IDataObject dataObject, IOleDropSource dropSource, Int32 allowedEffects, Int32[] finalEffect)
       at System.Windows.DragDrop.OleDoDragDrop(DependencyObject dragSource, DataObject dataObject, DragDropEffects allowedEffects)
       at System.Windows.DragDrop.DoDragDrop(DependencyObject dragSource, Object data, DragDropEffects allowedEffects)
       at MindFusion.Diagramming.Wpf.NodeListView.OnPreviewMouseMove(MouseEventArgs e)
       at System.Windows.UIElement.OnPreviewMouseMoveThunk(Object sender, MouseEventArgs e)
       at System.Windows.Input.MouseEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at MindFusionDemo2.App.Main() in c:\Users\vjong\Documents\Visual Studio 2012\Projects\DragDropExample\MindFusionDemo2\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

 



I've added a method that should show a message box with some text to the EventHandler but that method isn't called.

Thanks in advance!
Vincent
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Getting a null reference exception when dragging in a node derived from ShapeNode
Reply #7 - Oct 3rd, 2013 at 12:42pm
Print Post  
Check if the node you are dragging is an instance of that class. If you add a breakpoint to the Clone override, does it stop there?
  
Back to top
 
IP Logged
 
Vincent
Junior Member
**
Offline


I Love MindFusion!

Posts: 62
Joined: Oct 3rd, 2013
Re: Getting a null reference exception when dragging in a node derived from ShapeNode
Reply #8 - Oct 4th, 2013 at 7:07am
Print Post  
It works now! I had child classes of my own class that inherits from ShapeNode and I had to add the copy constructor there as well.

Thanks for all the help!  Grin

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