Answers.Body: <p>Drop the <code>getUrl()</code> from the <code>set</code> and everything works.  </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-02T17:10:01.200Z
Answers.LastActivityDate: 2018-06-04T11:18:01.790Z
Answers.LastEditDate: 2018-06-04T11:18:01.790Z
Answers.UserDisplayName: Hellyar
Body: <p>This is for an <code>index page</code> (again).  I want to list the 10 most recent articles.  There is a field <code>postImage</code>.  It is not required.  If it exists, I want to use <code>postImage</code>.  If not, I want to use a default image.</p>
  
  <p>The relevant code is under the <code>img</code> tag.   </p>
  
  <p>I get the following error:  <strong>Impossible to invoke a method ("getUrl") on a null variable.</strong>.  I can verify that 7 of the first 10 entries do have a <code>postImage</code>.  </p>
  
  <blockquote>
    <p>{% for item in craft.entries.section('posts').limit(10) %}
                                {% set pic = item.postImage.one().getUrl() %}
                                {% if loop.index is divisible by(2) %}</p>
  </blockquote>
  
  <pre><code>                        &lt;div class="card card-plain card-blog"&gt;
                              &lt;div class="row"&gt;
                                  &lt;div class="col-md-5"&gt;
                                      &lt;div class="card-header card-header-image"&gt;
                                          &lt;a href="#pablito"&gt;
  
                                              &lt;img class="img img-raised" src="{% if pic %}{{ pic }}{% else %}assets/img/office2.jpg
                                                  {% endif %}
                                              "&gt;    
  
                                          &lt;/a&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                  &lt;div class="col-md-7"&gt;
                                      &lt;h6 class="card-category text-info"&gt;Enterprise&lt;/h6&gt;
                                      &lt;h3 class="card-title"&gt;
                                          &lt;a href="{{ item.url }}"&gt;{{ item.title }}&lt;/a&gt;
                                      &lt;/h3&gt;
                                      &lt;p class="card-description"&gt;
                                         {{ item.introParagraph }}
                                          &lt;a href="{{ item.url }}"&gt; Read More &lt;/a&gt;
                                      &lt;/p&gt;
                                      &lt;p class="author"&gt;
                                          by
                                          &lt;a href=""&gt;
                                              &lt;b&gt;x&lt;/b&gt;
                                          &lt;/a&gt;, 2 days ago
                                      &lt;/p&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                          {% else %}
  </code></pre>
  
  <p>There are 2 <code>if</code> / <code>else</code> statements.   Only the 1 associated with the <code>img</code> tag is relevant.</p>
  
  <p>Thanks.</p>
  
Tags: <templating>
Title: Trying to template a post's image on an index page with a default image if it does not exist
ThreadText: # Trying to template a post's image on an index page with a default image if it does not exist
  
  (Asked by: username_0 on 2018-06-02)
  
  This is for an `index page` (again). I want to list the 10 most recent articles. There is a field `postImage`. It is not required. If it exists, I want to use `postImage`. If not, I want to use a default image.
  
  The relevant code is under the `img` tag.
  
  I get the following error: **Impossible to invoke a method ("getUrl") on a null variable.**. I can verify that 7 of the first 10 entries do have a `postImage`.
  
  > {% for item in craft.entries.section('posts').limit(10) %}
  > {% set pic = item.postImage.one().getUrl() %}
  > {% if loop.index is divisible by(2) %}
  
  ```
                          <div class="card card-plain card-blog">
                              <div class="row">
                                  <div class="col-md-5">
                                      <div class="card-header card-header-image">
                                          <a href="#pablito">
  
                                              <img class="img img-raised" src="{% if pic %}{{ pic }}{% else %}assets/img/office2.jpg
                                                  {% endif %}
                                              ">    
  
                                          </a>
                                      </div>
                                  </div>
                                  <div class="col-md-7">
                                      <h6 class="card-category text-info">Enterprise</h6>
                                      <h3 class="card-title">
                                          <a href="{{ item.url }}">{{ item.title }}</a>
                                      </h3>
                                      <p class="card-description">
                                         {{ item.introParagraph }}
                                          <a href="{{ item.url }}"> Read More </a>
                                      </p>
                                      <p class="author">
                                          by
                                          <a href="">
                                              <b>x</b>
                                          </a>, 2 days ago
                                      </p>
                                  </div>
                              </div>
                          </div>
  
                          {% else %}
  
  ```
  
  There are 2 `if` / `else` statements. Only the 1 associated with the `img` tag is relevant.
  
  Thanks.
  
  **Tags:** templating
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-06-02. Score: 1 | Accepted answer
  
  Drop the `getUrl()` from the `set` and everything works.
  
  
  ---
  
  