Skip to content
Object reference not set to an instance of an object

Object reference not set to an instance of an object

DodaTech 3 min read

The “Object reference not set to an instance of an object” error in PowerShell means you tried to access a property or method on a variable that is $null.

What It Means

PowerShell is built on .NET, where every object reference either points to an actual instance or is $null. When you access a property ($obj.Property) or call a method ($obj.Method()) on a $null reference, .NET throws a NullReferenceException — the most common runtime exception across all .NET languages. The error tells you a reference was null but does not tell you which variable, so you must trace back through your code to find the source.

Why It Happens

  • A cmdlet or function returned $null but you did not check the result.
  • A hash key or array index does not exist: $data["missing"].
  • A property on a nested object is null: $computer.BIOS.SerialNumber where $computer.BIOS is $null.
  • You assigned $null earlier and forgot: $result = $null.
  • An XML node or JSON field was not present in the parsed document.
  • A parameter was not provided: param($Name) called without $Name.

How to Fix It

1. Check for null before accessing properties

# Bad
$process = Get-Process -Name "nonexistent" -ErrorAction SilentlyContinue
$process.Name

# Good
$process = Get-Process -Name "nonexistent" -ErrorAction SilentlyContinue
if ($process) {
    $process.Name
} else {
    Write-Warning "Process not found"
}

2. Use the null-conditional operator (PowerShell 7+)

# Only available in PowerShell 7 and later
$computer.BIOS?.SerialNumber

# Returns $null instead of throwing if BIOS is null

3. Use default values with the null-coalescing operator

# PowerShell 7+
$name = $user.Name ?? "Unknown"

# Or in older versions
$name = if ($user) { $user.Name } else { "Unknown" }

4. Check before method calls

$services = Get-Service -Name "Nonexistent" -ErrorAction SilentlyContinue
if ($null -ne $services) {
    $services.Start()
}

5. Verify hash tables and arrays have the key or index

$data = @{ name = "Alice" }

# Bad — key doesn't exist
$data.email.ToString()

# Good — check first
if ($data.ContainsKey("email")) {
    $data.email.ToString()
}

# Or use TryGetValue
$value = $null
if ($data.TryGetValue("email", [ref]$value)) {
    $value.ToString()
}

6. Trace null sources with verbose output

Write-Debug "Variable `$result is: $($result | Out-String)"
$result.Property   # If this fails, the debug output shows what $result was
How is $null different from empty string or empty array?
$null means “no value exists.” An empty string "" is a value — it happens to be zero-length but still exists. An empty array @() is also a valid object. In boolean tests, $null is falsy, "" is falsy (PowerShell 7+ treats empty string as falsy), and @() is falsy only in PowerShell 7+. Use $null -eq $var to test specifically for null, not empty.
Does PowerShell 5 have null-conditional operators?
No — ?. and ?? are PowerShell 7+ features. In PowerShell 5, use if statements or the -And operator to guard: $a -and $a.Property. You can also write helper functions or use try/catch around null-prone calls to handle NullReferenceException gracefully.
Why doesn’t the error tell me which variable is null?
PowerShell surfaces the raw .NET NullReferenceException, which does not include variable names in the call stack. The stack trace shows line numbers and method names but not local variable values. Use debugging tools like Set-PSBreakpoint -Variable, Write-Debug, or -Debug parameter on cmdlets to inspect variable states at each line.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro