<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>

CC BY-SA 3.0
2016-08-30T18:36:44.330Z
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?
Hellyar
CC BY-SA 3.0
2016-08-30T20:18:19.583Z
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`.
Marion Newlevant
CC BY-SA 3.0
2016-08-30T03:33:04.317Z
2016-08-30T20:00:44.170Z
2016-08-30T20:00:44.170Z
Brad Bell
<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>

<templating><search>
Search Error Appears on Blank Searches
# 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') %}

Search Results for "{{ query }}"
{% if entries|length %}
    {% for post in entries %}
        
            
                
                
                    {{ post.title}}
                    {{ post.postDate | date('F d, Y') }} Nicole
                
            
        
    {% endfor %}
{% else %}
    You search for "{{ query }}" didn't retun any results
{% 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`.  

---

