Powershell – exception calling “…” with “…” argument(s): “Object reference not set to an instance of an object”

Not all is so good in the world of using .NET from powershell code as it could be seen at first glance. I have to write poweshell code that need to use .NET library which realize text user interface. Here you can find detailed info about this library – https://www.codeproject.com/Articles/750873/Turbo-Vision-resurrected-for-Csharp-and-with-XAML 

It is very nice and it realizes Turbo Vision like interface. Turbo Vision is old fashion text user interface library I am familiar with.  The main code does following:

function Get-ScriptPath
{
    Split-Path $myInvocation.ScriptName
}

$moduleName = “$(Get-ScriptPath)\Xaml.dll”
import-module $moduleName

$moduleName = “$(Get-ScriptPath)\ConsoleFramework.dll”
import-module $moduleName

$moduleName = “$(Get-ScriptPath)\Binding.dll”
import-module $moduleName

[ConsoleFramework.Controls.Window] $winHost =
    [ConsoleFramework.Controls.Window][ConsoleFramework.ConsoleApplication]::LoadFromXaml(“windows-host.xml”,$null)

#$window =  [ConsoleFramework.Controls.Window] $consoleApplication.LoadFromXaml(“$(Get-ScriptPath)\windows-host.xml”, [object]$null)

 

As you see dll files is manages .NET libraries that realize all needed functionality. Unfortunately this code produces error from the title as shown below

image

Google shows us that this error is causes by situation when somewhere in the .NET code null reference exception occurred. Also you can see that powershell doesn’t give you  any detailed info or stack trace that can be very important for debugging purpose. 

My analyze gives me info that problem is related to this code of the library:

var assembly = Assembly.GetEntryAssembly();

The problem is that call to the Assembly.GetEntryAssembly() returns null if it is called from unmanaged code. This behavior is described on MSDN and also there are discussion you can find in the Internet.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s