Answers.Body: <p>You could just check the length of the whole asset array first, to make sure there are two images. Something like this:</p>
  
  <pre><code>{% if (entry.igBanner | length) &gt; 1 %}
      {% set asset = entry.igBanner[1] %}
      &lt;img src={{ asset.url }} class="img-responsive" alt={{entry.title}}&gt;
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-23T07:51:25.523Z
Answers.LastActivityDate: 2016-03-23T07:51:25.523Z
Answers.UserDisplayName: Steve Holland
Body: <p>I have a single asset field that handles two kinds of images:</p>
  
  <p>1) Big Banner image
  2) Small featured image</p>
  
  <p>The problem is I want to check for the presence of the second image before I output it on the page. I am doing it like so:</p>
  
  <pre><code>   {% if entry.igBanner[1] | length %}
        {% set asset = entry.igBanner[1] %}
        &lt;img src={{ asset.url }} class="img-responsive" alt={{entry.title}}&gt;
      {% endif %}
  </code></pre>
  
  <p>But when I try to check the length of an empty index (where the image is not ), my template crashes saying I am trying to pull a method on a null variable. How do I check the presence of an image asset at index<code>[1]</code>?</p>
  
Tags: <templating><assets>
Title: How do I check for the presence of an asset before accessing an empty asset field?
ThreadText: # How do I check for the presence of an asset before accessing an empty asset field?
  
  (Asked by: username_0 on 2016-03-23)
  
  I have a single asset field that handles two kinds of images:
  
  1) Big Banner image
  2) Small featured image
  
  The problem is I want to check for the presence of the second image before I output it on the page. I am doing it like so:
  
  ```
     {% if entry.igBanner[1] | length %}
        {% set asset = entry.igBanner[1] %}
        <img src={{ asset.url }} class="img-responsive" alt={{entry.title}}>
      {% endif %}
  
  ```
  
  But when I try to check the length of an empty index (where the image is not ), my template crashes saying I am trying to pull a method on a null variable. How do I check the presence of an image asset at index`[1]`?
  
  **Tags:** templating, assets
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-03-23. Score: 3
  
  You could just check the length of the whole asset array first, to make sure there are two images. Something like this:
  
  ```
  {% if (entry.igBanner | length) > 1 %}
      {% set asset = entry.igBanner[1] %}
      <img src={{ asset.url }} class="img-responsive" alt={{entry.title}}>
  {% endif %}
  
  ```
  
  
  ---
  
  