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

&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 %}


{{ post.postImage.first().getUrl('thumbImage')}} works without error every place else.

Any ideas on what I am missing?
{{ 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 %}