Answers.Body: <p>Why don't you just check if there is at least one photo? Something along the lines of</p>
  
  <pre><code>'transformer' =&gt; function(EntryModel $entry) {
      return [
          'ID' =&gt; $entry-&gt;id,
          'Year' =&gt; $entry-&gt;year,
          'Manufacturer' =&gt; $entry-&gt;manufacturer,
          'Model' =&gt; $entry-&gt;model,
          'Category' =&gt; (string)$entry-&gt;category[0],
          'Pics' =&gt; !empty($entry-&gt;listingPhotos) ? $entry-&gt;listingPhotos[0]-&gt;url : null,
      ];
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-10T14:51:20.197Z
Answers.Text: I tried something similar to this earlier but it didn't work. I was able to get your code to work, but I had to add [0]-> to the argument:
  `'Pics' => !empty($entry->listingPhotos[0]->url) ? $entry->listingPhotos[0]->url : null,`
Answers.UserDisplayName: Noah Yamen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-10T09:51:11.640Z
Answers.LastActivityDate: 2017-02-10T09:51:11.640Z
Answers.UserDisplayName: Andris Sevcenko
Body: <p>These keeps erroring out if any of the entries' do not have any assets in the 'listingPhotos' field. However, IF all entries have at least 1 photo in the 'listingsPhotos' field, it works. </p>
  
  <p>The Error Message: Trying to get property of non-object </p>
  
  <pre><code>namespace Craft;
  
  return [
      'endpoints' =&gt; [
          'trucks.json' =&gt; [
              'elementType' =&gt; 'Entry',
              'criteria' =&gt; ['section' =&gt; 'trucks'],
              'paginate' =&gt; false,
              'transformer' =&gt; function(EntryModel $entry) {
                  return [
                      'ID' =&gt; $entry-&gt;id,
                      'Year' =&gt; $entry-&gt;year,
                      'Manufacturer' =&gt; $entry-&gt;manufacturer,
                      'Model' =&gt; $entry-&gt;model,
                      'Category' =&gt; (string)$entry-&gt;category[0],
                      'Pics' =&gt; $entry-&gt;listingPhotos[0]-&gt;url,
                  ];
              },
          ],
      ]
  ];
  </code></pre>
  
Tags: <entries><assets><plugin-elementapi>
Title: ElementAPI: Error's out if entry doesn't have assets
ThreadText: # ElementAPI: Error's out if entry doesn't have assets
  
  (Asked by: username_0 on 2017-02-09)
  
  These keeps erroring out if any of the entries' do not have any assets in the 'listingPhotos' field. However, IF all entries have at least 1 photo in the 'listingsPhotos' field, it works.
  
  The Error Message: Trying to get property of non-object
  
  ```
  namespace Craft;
  
  return [
      'endpoints' => [
          'trucks.json' => [
              'elementType' => 'Entry',
              'criteria' => ['section' => 'trucks'],
              'paginate' => false,
              'transformer' => function(EntryModel $entry) {
                  return [
                      'ID' => $entry->id,
                      'Year' => $entry->year,
                      'Manufacturer' => $entry->manufacturer,
                      'Model' => $entry->model,
                      'Category' => (string)$entry->category[0],
                      'Pics' => $entry->listingPhotos[0]->url,
                  ];
              },
          ],
      ]
  ];
  
  ```
  
  **Tags:** entries, assets, plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-02-10. Score: 3 | Accepted answer
  
  Why don't you just check if there is at least one photo? Something along the lines of
  
  ```
  'transformer' => function(EntryModel $entry) {
      return [
          'ID' => $entry->id,
          'Year' => $entry->year,
          'Manufacturer' => $entry->manufacturer,
          'Model' => $entry->model,
          'Category' => (string)$entry->category[0],
          'Pics' => !empty($entry->listingPhotos) ? $entry->listingPhotos[0]->url : null,
      ];
  }
  
  ```
  
  > **Comments:**
  > **username_0**: I tried something similar to this earlier but it didn't work. I was able to get your code to work, but I had to add [0]-> to the argument: `'Pics' => !empty($entry->listingPhotos[0]->url) ? $entry->listingPhotos[0]->url : null,`  
  
  ---
  
  