NullReferenceException after method return
<p>I have encountered a <code>NullReferenceException</code> in an <strong>odd location</strong>. You can see where it is in the picture:</p>

<p><a href="https://i.stack.imgur.com/Q6pIB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q6pIB.png" alt="Picture"></a></p>

<p>The exception was thrown outside of try catch and after return. And both <code>Move</code> and <code>MoveResult</code> data types are structures and are not reference type.</p>

<p>Here are the details of the exception:</p>

<pre><code>System.NullReferenceException was unhandled
      Message=Object reference not set to an instance of an object.
      Source=MatinChess.Net
      StackTrace:
           at MatinChess.Net.MatinChess.MovePiece(Move move) in C:\Users\Matin\Documents\GitHub\MatinChessDLL\dotnet\MatinChess.Net\MatinChess.cs:line 37
           at MatinChess.Net.Demo.Program.Main(String[] args) in C:\Users\Matin\Documents\GitHub\MatinChessDLL\dotnet\MatinChess.Net.Demo\Program.cs:line 31
           at System.AppDomain._nExecuteAssembly(Assembly 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.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
</code></pre>

<p>I am using .NET Framework 2.0 on Visual Studio 2015 community edition.</p>

<h2>Edit</h2>

<p>When I comment the <code>ExternMethods.MovePiece</code> line, it does not throw the exception.</p>

<p>Here is the definition of this method:</p>

<pre><code>[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public extern static void MovePiece(Move movement, ref MoveResult result);
</code></pre>