question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullReferenceException in console when running code with PS classes

See original GitHub issue

System Details

System Details Output

### VSCode version: 1.38.1 b37e54c98e1a74ba89e03073e5a3761284e3ffb0 x64

### VSCode extensions:
ckolkman.vscode-postgres@1.1.11     
eamodio.gitlens@10.1.0
KnisterPeter.vscode-github@0.30.2   
ms-vscode.powershell@2019.9.0       
vscode-icons-team.vscode-icons@9.4.0


### PSES version: 1.13.0.0

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      6.2.3
PSEdition                      Core
GitCommitId                    6.2.3
OS                             Microsoft Windows 10.0.18362 
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}        
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Description

I’m running into a NullReferenceException when running the code in VSCode Powershell extension console. I experienced this error on both Mac and Windows, though the further debug information only comes from Windows. It’s worth mentioning, that the error start to appear after a noticeable amount of time, which makes it hard to debug, however I will try my best.

I’m running Pester tests, which involve accessing classes defined inside the module. After running tests for a while and after a random time period I’m starting to get unexpected output in the console which is completely unrelated to the tests I’m running. Two tests that were run without changing the tested code would return different results: one would succeed, the next one will fail with a NullReference. Once it’s failed, it would always fail until I restart the PS console, which resolve the issue temporarily, until next time.

      NullReferenceException: Object reference not set to an instance of an object.
      at InitFile, C:\git\dbops\internal\classes\DBOps.class.ps1: line 959
      at DBOpsFile, C:\git\dbops\internal\classes\DBOps.class.ps1: line 913
      at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 54

The code line causing the issue seems to be this one:

[DBOpsHelper]::GetBinaryFile($someFile)

which is working perfectly fine if I’m running it manually. It actually works without an issue in a clean session, until it doesn’t. The pattern in which I was able to repro this bug looks like this:

  • Run all the tests in the module
  • Wait several minutes
  • Run one single class-related test -> Success
  • Wait several minutes
  • Run the same test -> Failure

Last but not least, this error never occurs in a regular Powershell session regardless of PS version.

Expected Behaviour

Code runs without raising a NullRef

Actual Behaviour

NullReferenceException: Object reference not set to an instance of an object.

>> $error[1]| select *

PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean
                        propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
                           at System.Management.Automation.ScriptBlock.InvokeAsMemberFunctionT[T](Object instance, Object[] args)
                           at System.Management.Automation.Internal.ScriptBlockMemberMethodWrapper.InvokeHelperT[T](Object instance, Object sessionStateInternal, Object[] args)
                           at CallSite.Target(Closure , CallSite , Type , Object )
                           at InitFile(Closure , FunctionContext )
TargetObject          :
CategoryInfo          : OperationStopped: (:) [], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at InitFile, C:\git\dbops\internal\classes\DBOps.class.ps1: line 959
                        at DBOpsFile, C:\git\dbops\internal\classes\DBOps.class.ps1: line 913
                        at DBOpsPackage, C:\git\dbops\internal\classes\DBOps.class.ps1: line 538
                        at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 154
                        at Invoke-Blocks, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\SetupTeardown.ps1: line 140
                        at Invoke-TestCaseSetupBlocks, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\SetupTeardown.ps1: line 115
                        at Invoke-Test, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\It.ps1: line 279
                        at ItImpl, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\It.ps1: line 231
                        at It, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\It.ps1: line 121
                        at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 209
                        at DescribeImpl, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\Describe.ps1: line 171
                        at Context, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\Context.ps1: line 72
                        at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 152
                        at DescribeImpl, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\Describe.ps1: line 171
                        at Describe, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\Describe.ps1: line 85
                        at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 31
PipelineIterationInfo : {}

>> $error[1].Exception| select *

Message        : Object reference not set to an instance of an object.
Data           : {}
InnerException :
TargetSite     : Void InvokeWithPipe(Boolean, ErrorHandlingBehavior, System.Object, System.Object, System.Object, System.Management.Automation.Internal.Pipe, System.Management.Automation.InvocationInfo, Boolean, System.Collections.Generic.List`1[System.Management.Automation.PSVariable],  
                 System.Collections.Generic.Dictionary`2[System.String,System.Management.Automation.ScriptBlock], System.Object[])
StackTrace     :    at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop,     
                 List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
                    at System.Management.Automation.ScriptBlock.InvokeAsMemberFunctionT[T](Object instance, Object[] args)
                    at System.Management.Automation.Internal.ScriptBlockMemberMethodWrapper.InvokeHelperT[T](Object instance, Object sessionStateInternal, Object[] args)
                    at CallSite.Target(Closure , CallSite , Type , Object )
                    at InitFile(Closure , FunctionContext )
HelpLink       :
Source         : System.Management.Automation
HResult        : -2147467261

Attached Logs

1570427510-af47caf0-8890-45d9-b195-6d2e3883fba41570419200265.zip

Let me know if there’s anything else I can submit. I didn’t see any related errors in the VSCode plugin so I would appreciate any advice.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
rjmholtcommented, Oct 7, 2019

This changes the affinity of the class to the now disposed runspace, causing this exception

Yeah that’s what I was imagining was happening – was trying to think what would cause it. PSSA would explain it, especially given the delay

0reactions
nvarscarcommented, Oct 7, 2019

Hi @nvarscar, this is certainly an odd error. How are you running the Pester tests? With the Pester code lens or with Invoke-Pester?

I’m running Invoke-Pester or simply via F5 that launches the test file in a debug mode.

Would you be able to share that output?

I did. All I’m getting is: NullReferenceException: Object reference not set to an instance of an object. Every command that touches that piece of code from now on will return that nullref.

Is it possible to share the source that the call references?

This is the method code: https://github.com/sqlcollaborative/dbops/blob/development/internal/classes/DBOpsHelper.class.ps1#L17 And this is how it’s called: https://github.com/sqlcollaborative/dbops/blob/development/internal/classes/DBOps.class.ps1#L959 I tried very hard to replicate it in a simplified scenario, but never succeeded. It just works whenever I use it outside the module scope.

@nvarscar What kind of PSScriptAnalyzer rules are you running?

There’s a non-default rule (or rules) that ends up importing modules in another (temporary) runspace. This changes the affinity of the class to the now disposed runspace, causing this exception.

If you have a ScriptAnalyzerSettings.psd1 file for the repo, try removing it and see if you still get the error.

The thing is, I disabled the PSScriptAnalyzer validations a while back as they had been deadlocking on Appveyor. I don’t have ScriptAnalyzerSettings.psd1 and this is how I’ve been calling the PSSA tests: https://github.com/sqlcollaborative/dbops/blob/development/tests/dbops.Tests.ps1#L42 Anyways, that test is commented out right now and the error still occurs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle C# NullReferenceException? Examples
Well, the first is what we do to handle any exception. Its using C# try-catch. Use try catch statement around the code which...
Read more >
system.nullreferenceexception: object reference not set to an ...
Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it...
Read more >
Object Reference Not Set to an Instance of an Object - Stackify
This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a member— ......
Read more >
C# Null Reference Exception - Stack Overflow
I tried this in simple console application and it worked fine. ... Use the following code in your Threading Class, instead of
Read more >
Object reference not set to an instance of an object” error?
As mentioned earlier, the NullReferenceException indicates that your code is trying to work with an object that has a null value as its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found