Answers.Body: <blockquote>
    <p>{{ post.postImage.first().getUrl('thumbImage')}}</p>
  </blockquote>
  
  <p>Whatever entry <code>post</code> is referencing for whatever search term you're using when you get the error, doesn't have any <code>postImage</code> assets selected.</p>
  
  <p>So when you call <code>post.postImage.first()</code>, that returns <code>null</code> and you can't call <code>getUrl</code> on <code>null</code>.</p>
  
  <p>You can either make <code>postImage</code> required and make sure that all existing entries have at least one asset selected, or guard against it in Twig via:</p>
  
  <pre><code>{% set image = post.postImage.first() %}
  
  {% if image|length %}
      {{ image.getUrl('thumbImage') }}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-30T18:36:44.330Z
Answers.Text: So, `postImage` is a required field for every entry (post) in the `posts` section.  The null is being thrown by a specific search fragment - taken directly from an entry that has a `postImage`.   Searching `an` or `an/` throws a null even though they are in the body of the post.   Searching `an/apn` or `an/apn-`, which are still fragments, will output the post (image and all) in the search results.  So, what is it about `an` and `an/` that throws the null?
Answers.UserDisplayName: Hellyar
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-30T20:18:19.583Z
Answers.Text: You should guard against it in twig (always). Even if you make it a required field, someone can delete the asset out from under you.
  
  In this case, you are searching not only entries in the `post` section, but other entries as well. One of them is matching, and it doesn't have a `postImage`.
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-30T03:33:04.317Z
Answers.LastActivityDate: 2016-08-30T20:00:44.170Z
Answers.LastEditDate: 2016-08-30T20:00:44.170Z
Answers.UserDisplayName: Brad Bell
Body: <blockquote>
    <p>Impossible to invoke a method ("getUrl") on a null variable</p>
  </blockquote>
  
  <p>That's the error message generated by a blank search and some searches.   </p>
  
  <p>Fuzzy search is enabled in <code>config/general.php</code>, and it works.  The search template is straight from the Craft CMS "search form" tutorial.  </p>
  
  <pre><code>{% set query = craft.request.getParam('q') %}
  {% set entries = craft.entries.search(query).order('score') %}
  
  &lt;h2&gt;Search Results for "{{ query }}"&lt;/h2&gt;
  {% if entries|length %}
      {% for post in entries %}
          &lt;div class="box col-sm-3"&gt;
              &lt;div class="post-box vertical clearfix"&gt;
                  &lt;a href="{{ post.url }}" class="image-link arrow-icon dark-overlay"&gt;&lt;img src="{{ post.postImage.first().getUrl('thumbImage')}}" width="434" height="434" alt=""&gt;&lt;/a&gt;
                  &lt;div class="extra-info"&gt;
                      &lt;h5&gt;&lt;a href="{{ post.url }}"&gt;{{ post.title}}&lt;/a&gt;&lt;/h5&gt;
                      &lt;p class="meta small"&gt;{{ post.postDate | date('F d, Y') }} &lt;a href="#"&gt;Nicole&lt;/a&gt;&lt;/p&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      {% endfor %}
  {% else %}
      &lt;p&gt;You search for "{{ query }}" didn't retun any results&lt;/p&gt;
  {% endif %}
  </code></pre>
  
  <p><code>{{ post.postImage.first().getUrl('thumbImage')}}</code> works without error every place else.</p>
  
  <p>Any ideas on what I am missing?</p>
  
Tags: <templating><search>
Title: Search Error Appears on Blank Searches
ThreadText: # Search Error Appears on Blank Searches
  
  (Asked by: username_0 on 2016-08-30)
  
  > Impossible to invoke a method ("getUrl") on a null variable
  
  That's the error message generated by a blank search and some searches.
  
  Fuzzy search is enabled in `config/general.php`, and it works. The search template is straight from the Craft CMS "search form" tutorial.
  
  ```
  {% set query = craft.request.getParam('q') %}
  {% set entries = craft.entries.search(query).order('score') %}
  
  <h2>Search Results for "{{ query }}"</h2>
  {% if entries|length %}
      {% for post in entries %}
          <div class="box col-sm-3">
              <div class="post-box vertical clearfix">
                  <a href="{{ post.url }}" class="image-link arrow-icon dark-overlay"><img src="{{ post.postImage.first().getUrl('thumbImage')}}" width="434" height="434" alt=""></a>
                  <div class="extra-info">
                      <h5><a href="{{ post.url }}">{{ post.title}}</a></h5>
                      <p class="meta small">{{ post.postDate | date('F d, Y') }} <a href="#">Nicole</a></p>
                  </div>
              </div>
          </div>
      {% endfor %}
  {% else %}
      <p>You search for "{{ query }}" didn't retun any results</p>
  {% endif %}
  
  ```
  
  `{{ post.postImage.first().getUrl('thumbImage')}}` works without error every place else.
  
  Any ideas on what I am missing?
  
  **Tags:** templating, search
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-30. Score: 2 | Accepted answer
  
  > {{ post.postImage.first().getUrl('thumbImage')}}
  
  Whatever entry `post` is referencing for whatever search term you're using when you get the error, doesn't have any `postImage` assets selected.
  
  So when you call `post.postImage.first()`, that returns `null` and you can't call `getUrl` on `null`.
  
  You can either make `postImage` required and make sure that all existing entries have at least one asset selected, or guard against it in Twig via:
  
  ```
  {% set image = post.postImage.first() %}
  
  {% if image|length %}
      {{ image.getUrl('thumbImage') }}
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: So, `postImage` is a required field for every entry (post) in the `posts` section.  The null is being thrown by a specific search fragment - taken directly from an entry that has a `postImage`.   Searching `an` or `an/` throws a null even though they are in the body of the post.   Searching `an/apn` or `an/apn-`, which are still fragments, will output the post (image and all) in the search results.  So, what is it about `an` and `an/` that throws the null?  
  > **username_2**: You should guard against it in twig (always). Even if you make it a required field, someone can delete the asset out from under you.  In this case, you are searching not only entries in the `post` section, but other entries as well. One of them is matching, and it doesn't have a `postImage`.  
  
  ---
  
  