title: You cannot call a method on a null-valued expression
body: <p>I am simply trying to create a powershell script which calculates the md5 sum of an executable (a file).</p>
  
  <p>My .ps1 script:</p>
  
  <pre><code>$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."
  }
  </code></pre>
  
  <p>Upon running my script I receive the following error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>To my understanding, this error means the script is attempting to do something, but another part of the script does not have any information to permit the first part of the script to work properly. In this case, <code>$hash</code>.</p>
  
  <p><code>Get-ExecutionPolicy</code> outputs <code>Unrestricted</code>.</p>
  
  <p><strong>What is causing this error?</strong><br>
  <strong>What exactly is my null valued expression?</strong></p>
  
  <p>Any help is appreciated. I apologize if this is trivial and will continue my research.</p>
  
  <hr>
  
  <p>References: </p>
  
  <p><a href="http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/27/troubleshoot-the-invokemethodonnull-error-with-powershell.aspx" rel="noreferrer">http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/27/troubleshoot-the-invokemethodonnull-error-with-powershell.aspx</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell">How to get an MD5 checksum in PowerShell</a></p>