Answers.Body: <p>I figured it out. Not every entry uses the matrix block, the error appears when the script hits that empty/non-existing block (null) it can't go looking for something inside it. Adding a conditional around the foreach solves it.</p>
  
  <pre><code>&lt;?php
  
  use craft\elements\Entry;
  
  return [
    'endpoints' =&gt; [
      'discproj.json' =&gt; function() {
        return [
          'elementType' =&gt; Entry::class,
          'criteria' =&gt; ['section' =&gt; 'dPs'],
          'transformer' =&gt; function(Entry $entry) {
  
            $linkBlocks = null;
            if ($entry-&gt;proLinks) {
              foreach ($entry-&gt;proLinks-&gt;all() as $block) {
                switch ($block-&gt;type-&gt;handle) {
                  case 'external':
                  $linkBlocks[] = [
                    'externalTitle' =&gt; $block-&gt;externalTitle,
                    'externalLink' =&gt; $block-&gt;externalLink,
                  ];
                  break;
                }
              }
            }
  
            return [
              'projectLinks' =&gt; $linkBlocks,
            ];
          },
        ];
      },
    ]
  ];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-21T05:58:42.503Z
Answers.LastActivityDate: 2019-01-21T06:06:51.587Z
Answers.LastEditDate: 2019-01-21T06:06:51.587Z
Answers.UserDisplayName: H.W. Sanden
Body: <p>I get this JSON error when trying to output content of a matrix block called proLinks using the Element API.</p>
  
  <blockquote>
    <p>Call to a member function all() on null</p>
  </blockquote>
  
  <p>The code is as follows (I've shortened it and removed the other content, this is the only one that I can't get to work):</p>
  
  <pre><code>&lt;?php
  
  use craft\elements\Entry;
  
  return [
    'endpoints' =&gt; [
      'discproj.json' =&gt; function() {
        return [
          'elementType' =&gt; Entry::class,
          'criteria' =&gt; ['section' =&gt; 'dPs'],
          'transformer' =&gt; function(Entry $entry) {
  
            $linkBlocks = [];
            foreach ($entry-&gt;proLinks-&gt;all() as $block) {
              switch ($block-&gt;type-&gt;handle) {
                case 'external':
                $linkBlocks[] = [
                  'externalTitle' =&gt; $block-&gt;externalTitle,
                  'externalLink' =&gt; $block-&gt;externalLink,
                ];
                break;
              }
            }
  
            return [
              'projectLinks' =&gt; $linkBlocks,
            ];
          },
        ];
      },
    ]
  ];
  </code></pre>
  
  <p>I've looked at the other questions about this here, and they all seem to do things a little bit different from each other. I think I have the general structure, but there is obviously something missing.</p>
  
Tags: <craft3><plugin-elementapi>
Title: Matrix block Element API call on null error
ThreadText: # Matrix block Element API call on null error
  
  (Asked by: username_0 on 2019-01-20)
  
  I get this JSON error when trying to output content of a matrix block called proLinks using the Element API.
  
  > Call to a member function all() on null
  
  The code is as follows (I've shortened it and removed the other content, this is the only one that I can't get to work):
  
  ```
  <?php
  
  use craft\elements\Entry;
  
  return [
    'endpoints' => [
      'discproj.json' => function() {
        return [
          'elementType' => Entry::class,
          'criteria' => ['section' => 'dPs'],
          'transformer' => function(Entry $entry) {
  
            $linkBlocks = [];
            foreach ($entry->proLinks->all() as $block) {
              switch ($block->type->handle) {
                case 'external':
                $linkBlocks[] = [
                  'externalTitle' => $block->externalTitle,
                  'externalLink' => $block->externalLink,
                ];
                break;
              }
            }
  
            return [
              'projectLinks' => $linkBlocks,
            ];
          },
        ];
      },
    ]
  ];
  
  ```
  
  I've looked at the other questions about this here, and they all seem to do things a little bit different from each other. I think I have the general structure, but there is obviously something missing.
  
  **Tags:** craft3, plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-01-21. Score: 1 | Accepted answer
  
  I figured it out. Not every entry uses the matrix block, the error appears when the script hits that empty/non-existing block (null) it can't go looking for something inside it. Adding a conditional around the foreach solves it.
  
  ```
  <?php
  
  use craft\elements\Entry;
  
  return [
    'endpoints' => [
      'discproj.json' => function() {
        return [
          'elementType' => Entry::class,
          'criteria' => ['section' => 'dPs'],
          'transformer' => function(Entry $entry) {
  
            $linkBlocks = null;
            if ($entry->proLinks) {
              foreach ($entry->proLinks->all() as $block) {
                switch ($block->type->handle) {
                  case 'external':
                  $linkBlocks[] = [
                    'externalTitle' => $block->externalTitle,
                    'externalLink' => $block->externalLink,
                  ];
                  break;
                }
              }
            }
  
            return [
              'projectLinks' => $linkBlocks,
            ];
          },
        ];
      },
    ]
  ];
  
  ```
  
  
  ---
  
  