<p>You should check if there is a next and previous element. Most likely you reach the end or the beginning of your list and there is no element thus these variables are null so you receive an exception when you try to grab attributes from <code>null</code></p>

CC BY-SA 4.0
2018-07-09T06:13:13.170Z
Except that entry titles are being returned, just not `url` or `slug`.
Tyssen
CC BY-SA 4.0
2018-07-09T06:14:22.423Z
What do you do to display them? Only `{{ nextEntry }}`?
Robin Schambach
CC BY-SA 4.0
2018-07-09T06:23:38.470Z
If I do `{{ nextEntry }}` I get output like `Entry Title`. If I do `{{ nextEntry.url }}` or `{{ nextEntry.slug }}` I want to get `entry-title` but instead get the error.
Tyssen
CC BY-SA 4.0
2018-07-09T06:25:50.070Z
Because `{{ nextEntry }}` will trigger the objects `__toString` function. You don't actually access the real `title` attribute as with `{{ nextEntry.title }}`. Without that function you would receive an `object can not converted to string` Exception. You need to check if the variable is null or not. Short syntax would be `{{ nextEntry.url ?? '' }}`
Robin Schambach
CC BY-SA 4.0
2018-07-09T06:33:10.520Z
Sorry, you've lost me. If I do `{% set prevEntry = item.prev.slug %}` and check if `prevEntry` exists, I get the same type of error. If `item.prev` produces *something*, then surely the `slug` wouldn't be null because it's returning information about a valid entry that exists with a slug.
Tyssen
CC BY-SA 4.0
2018-07-09T06:34:29.897Z
In this case `item.prev` is null. `Impossible to access an attribute ("url") on a null variable.` is always the same kind of error.... you should google it, you'll receive plenty of recerences and all are solved in the very same way
Robin Schambach
CC BY-SA 4.0
2018-07-09T05:53:25.330Z
2018-07-09T05:53:25.330Z
Robin Schambach
<p>I have a search results page set up like this:</p>

<pre><code>{% set searchResults = craft.entries(queryParams) %}
{% paginate searchResults as pageInfo, pageEntries %}

{% if pageEntries|length %}
  {% for yacht in pageEntries %}
    {% set prevEntry = item.prev %}
    {% set nextEntry = item.next %}
    &lt;a href="{{ item.url }}
    {% if nextEntry or prevEntry %}?{%endif%}
    {% if nextEntry %}next={{nextEntry}}{%endif%}
    {% if prevEntry and nextEntry %}&amp;{% endif %}
    {% if prevEntry %}prev={{prevEntry}}{%endif%}"
    &gt;Link&lt;/a&gt;
  {% endfor %}
{% endif %}
</code></pre>

<p>That works fine except that it outputs the entry title of the next and previous entries. If I do <code>nextEntry.url</code> or <code>prevEntry.url</code> (or <code>slug</code>) I get:</p>

<blockquote>
  <p>Impossible to access an attribute ("url") on a null variable.</p>
</blockquote>

<p>Why is that?</p>

<elementcriteriamodel><previous-next>
Impossible to access an attribute ("url") on a null variable in search results
# Impossible to access an attribute ("url") on a null variable in search results

(Asked by: username_0 on 2018-07-09)

I have a search results page set up like this:
{% set searchResults = craft.entries(queryParams) %}
{% paginate searchResults as pageInfo, pageEntries %}

{% if pageEntries|length %}
  {% for yacht in pageEntries %}
    {% set prevEntry = item.prev %}
    {% set nextEntry = item.next %}
    Link
  {% endfor %}
{% endif %}

That works fine except that it outputs the entry title of the next and previous entries. If I do nextEntry.url or prevEntry.url (or slug) I get:

Impossible to access an attribute ("url") on a null variable.

Why is that?

**Tags:** elementcriteriamodel, previous-next

---

## 1 answers

### Answer by username_1 on 2018-07-09. Score: 1 | Accepted answer

You should check if there is a next and previous element. Most likely you reach the end or the beginning of your list and there is no element thus these variables are null so you receive an exception when you try to grab attributes from `null`

> **Comments:**
> **username_0**: Except that entry titles are being returned, just not `url` or `slug`.  
> **username_1**: What do you do to display them? Only `{{ nextEntry }}`?  
> **username_0**: If I do `{{ nextEntry }}` I get output like `Entry Title`. If I do `{{ nextEntry.url }}` or `{{ nextEntry.slug }}` I want to get `entry-title` but instead get the error.  
> **username_1**: Because `{{ nextEntry }}` will trigger the objects `__toString` function. You don't actually access the real `title` attribute as with `{{ nextEntry.title }}`. Without that function you would receive an `object can not converted to string` Exception. You need to check if the variable is null or not. Short syntax would be `{{ nextEntry.url ?? '' }}`  
> **username_0**: Sorry, you've lost me. If I do `{% set prevEntry = item.prev.slug %}` and check if `prevEntry` exists, I get the same type of error. If `item.prev` produces *something*, then surely the `slug` wouldn't be null because it's returning information about a valid entry that exists with a slug.  
> **username_1**: In this case `item.prev` is null. `Impossible to access an attribute ("url") on a null variable.` is always the same kind of error.... you should google it, you'll receive plenty of recerences and all are solved in the very same way  

---

