title: % : You cannot call a method on a null-valued expression
body: <p>I think I've found unexplainable in PowerShell scripting. Let me describe the problem below:</p>
  
  <p>I created a function that will query VSTS and will output back the amount of bugs that contain specific tags.</p>
  
  <pre class="lang-cs prettyprint-override"><code>function GetBugsFromVSTS($applicationName, $first_url, $second_url, $query) {
      #Building the body of the HTTP request to VSTS endpoint
      $bodyOfRequest = $query | ConvertTo-Json
  
      #Building headers of the HTTP request to VSTS endpoint
      $headers = @{
          'Content-Type'='application/json'
          'Accept'='application/json'
          'Authorization' = '' + $vsts_access_token + ''
      }
  
      $getBugs = Invoke-RestMethod -Uri $first_url `
                 -Method Post `
                 -Body $bodyOfRequest `
                 -Headers $headers | ConvertTo-Json -Depth 100
  
      $json_decoded = $getBugs | ConvertFrom-Json
  
      if ($json_decoded.workItems -eq $null) {
          Write-Host "$applicationName Json decoded workitems are equal null" -ForegroundColor Red
      } else {
          Write-Host "$applicationName Json decoded workitems are not null" -ForegroundColor Green
          $json_decoded.workItems | % {
              $bug_id = $_.id
              Write-Host $bug_id
          }
      }
  
      $json_decoded.workItems | % {
          $bug_id = $_.id
          $url = ($second_url +$bug_id)
  
          $getVstsBugState = Invoke-RestMethod -Uri $url -Method Get -Headers $headers
  
          [PSCustomObject]@{
              "Application Name" = $applicationName
              "Id" = $bug_id;
              "State" = $getVstsBugState.fields.'System.State';
              "Reason" = $getVstsBugState.fields.'System.Reason';
              "Severity" = $getVstsBugState.fields.'Microsoft.VSTS.Common.Severity'.Substring(4);
              "AssignedTo" = $getVstsBugState.fields.'System.AssignedTo'.displayName
          }
      }
  }
  </code></pre>
  
  <p>When I try to run it</p>
  
  <pre><code>    $application = "MyBeautifulApp"
      $application_first_url = "https://test.visualstudio.com/72L80E4b-1583-45c1-b669-d8de6133V895/_apis/wit/wiql?api-version=1.0"
      $application_second_url = "https://test.visualstudio.com/72L80E4b-1583-45c1-b669-d8de6133V895/_apis/wit/workItems/"
      $application_query = @{"query" = "SELECT [System.Id],[System.WorkItemType],[System.Title],[System.AssignedTo],[System.State] FROM WorkItems WHERE [System.TeamProject] = @project AND [System.WorkItemType] = 'Bug' AND [System.Title] CONTAINS 'Test' AND [System.TAGS] CONTAINS 'Test'"}
      GetBugsFromVSTS $application $application_first_url $application_second_url $application_query
  } catch {
      Write-Warning "##vso[task.logissue type=warning;]Some problem occured querying MyBeautifulApp application. Please see below for error details"
      $_
  }
  </code></pre>
  
  <p>The most interesting part here is that I'm getting this really weird error message</p>
  
  <blockquote>
    <p>WARNING: ##vso[task.logissue type=warning;]Some problem occured
    querying MyBeautifulApp application. Please see below for error
    details</p>
    
    <p>% : You cannot call a method on a null-valued expression.</p>
  </blockquote>
  
  <p>For the sake of test I added <code>if..else</code> clause to test that <code>$json_decoded.workItems</code> are not equal null, and that verification passes. If I run the code, full output will be</p>
  
  <pre>
  MyBeautifulApp Json decoded workitems are not null
  9805
  10330
  10331
  10371
  10372
  10373
  10374
  
  WARNING: ##vso[task.logissue type=warning;]Some problem occured querying MyBeautifulApp application. Please see below for error details
  % : You cannot call a method on a null-valued expression. line 29
  
  +     $json_decoded.workItems | % {
  +                               ~~~
      + CategoryInfo          : InvalidOperation: (:) [ForEach-Object], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull,Microsoft.PowerShell.Commands.ForEachObjectCommand
  </pre>
  
  <p>How is it possible that the first verification for <code>$json_decoded.workItems</code> (<strong>line 23</strong>) is not indicating that variable equals null, whereas the second statement (<strong>line 29</strong>) shows that it's null?</p>