Quick Tips to Fix the Error “you cannot call a method on a null valued expression in powershell”

Powershell is a cross-platform, open-source, and CLI (command-line interface) tool and an automation object-oriented engine as well as a scripting language. It allows programmers, developers, and IT admins to automate configurations and tasks. It is a management framework that is a combination of both command-line and scripting language. Powershell is built on the .net framework and .net core framework. Programmers and developers all over the world use powershell to create various programs. When it is widely used, it is normal to encounter errors as well. You may experience “you cannot call a method on a null valued expression in powershell”.

You must be wondering what has brought the error pop up. We are here to help you solve the error so you don’t need to worry anymore. It’s the motto of Tutopal to help you get rid of the error warning. First, let’s check out how the error occurs

How the error shows up

When you try to create a script to calculate the md5 sum of a file, you get the error message. Have a look at the program code that returns the error

$answer = Read-Host "File name and extension (ie; file.exe)"
$someFilePath = "C:\Users\xxx\Downloads\$answer"

If (Test-Path $someFilePath){
                        $stream = [System.IO.File]::Open("$someFilePath",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
                        $hash = [System.BitConverter]::ToString($md5.ComputeHash($stream))
                        $hash
                        $stream.Close()
                        }
Else{
Write-Host "Sorry, file $answer doesn't seem to exist."
}

As soon as you run the script, you get the following error message

You cannot call a method on a null-valued expression.
At C:\Users\xxx\Downloads\md5sum.ps1:6 char:29
+                             $hash = [System.BitConverter]::ToString($md5.Compute ...
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

And that’s exactly how you end up with the error warning.

How To Fix the Error “you cannot call a method on a null valued expression in powershell”

Null is an undeclared variable, and in this case, it’s $md5. You have commented it on the same place, but it should be declared some other place. See how you have declared it

$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider

The reason for the error popping up is running a method that has not existed.

PS C:\Users\Matt> $md5 | gm


TypeName: System.Security.Cryptography.MD5CryptoServiceProvider

Name MemberType Definition
---- ---------- ----------
Clear Method void Clear()
ComputeHash Method byte[] ComputeHash(System.IO.Stream inputStream), byte[] ComputeHash(byte[] buffer), byte[] ComputeHash(byte[] buffer, int offset, ...

Solution to Fix

The $md5.ComputeHash()’s .ComputeHash() was known to be a null-valued expression. You get the same effect if you type gibberish. Look at the code to solve the error

PS C:\Users\Matt> $bagel.MakeMeABagel()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $bagel.MakeMeABagel()
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Conclusion

In this article, we highlighted the solution to remove the error “you cannot call a method on a null valued expression in powershell” so that you can code the program the way you want.

I hope you find it helpful! Don’t hesitate to drop a message in the below comment box if you need any assistance!

Leave a Reply

Your email address will not be published. Required fields are marked *