<p>For whatever reason, your site isn't able to find the image. And since your Twig code isn't being <em>defensive</em>, it's failing ungracefully.</p>

<pre><code>{% set image = block.media.first() %}
</code></pre>

<p>This first part is fine... I'm assuming that <code>media</code> is an Assets relationship field. You're attempting to fetch the <code>first</code> asset, and set that to <code>image</code>. Here's the kicker... <strong>when no assets are found, <code>image</code> will be set to NULL</strong>.</p>

<p>This next part is where your problem is...</p>

<pre><code>{{ image.url }}
</code></pre>

<p>That'll work great assuming an asset was found. But when no assets were found, this will trigger an error. Because <code>image</code> would be set to NULL, and NULL doesn't have an attribute of <code>url</code>. (This is exactly what the error message is telling you.)</p>

<p>Instead, you should write that line more defensively...</p>

<pre><code>{{ image ? image.url }}
</code></pre>

<p>This technique uses the <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="nofollow">ternary operator</a> to first check "if <code>image</code> is truthy" before attempting to output <code>image.url</code>. In other words, if <code>image</code> contains a valid asset, output the image URL.</p>

<p>That way, if no asset is found, you won't get a big nasty error message.</p>

<hr>

<p>On a separate note... You seem to be expressing some urgency that your site has gone down. But the screenshot you showed is clearly a byproduct of being in <a href="http://buildwithcraft.com/help/dev-mode" rel="nofollow"><code>devMode</code></a>. For the record, you never want to have <code>devMode</code> enabled in a live production environment.</p>

CC BY-SA 3.0
2015-09-26T19:51:06.260Z
2015-09-26T19:51:06.260Z
Lindsey D
<p>It's a bit hard to tell from this snippet, but if <code>image</code> is an asset field it's important to remember that these are returned as arrays so you have to access the url more like <code>block.image.first().url</code>. However, if the image variable is set elsewhere this probably won't help.</p>

<p>EDIT: <a href="http://buildwithcraft.com/docs/assets-fields#templating" rel="nofollow">Relevant docs</a></p>

CC BY-SA 3.0
2015-09-26T15:54:26.090Z
2015-09-26T15:54:26.090Z
brianjhanson
<p>My site just suddenly broke down on a certain page with the following error code, (see screenshot).</p>

<p><a href="https://i.sstatic.net/loQvg.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/loQvg.png" alt="enter image description here"></a></p>

<p>It is occuring for every image on this page and really confusing me.</p>

<p>Any quick suggestions?</p>

<error><error-templates><error-handling>
Getting image url error code: Impossible to access an attribute ("url") on a null variable
# Getting image url error code: Impossible to access an attribute ("url") on a null variable

(Asked by: username_0 on 2015-09-26)

My site just suddenly broke down on a certain page with the following error code, (see screenshot).

[![enter image description here](https://i.sstatic.net/loQvg.png)](https://i.sstatic.net/loQvg.png)

It is occuring for every image on this page and really confusing me.

Any quick suggestions?

**Tags:** error, error-templates, error-handling

---

## 2 answers

### Answer by username_1 on 2015-09-26. Score: 3

For whatever reason, your site isn't able to find the image. And since your Twig code isn't being defensive, it's failing ungracefully.
{% set image = block.media.first() %}

This first part is fine... I'm assuming that media is an Assets relationship field. You're attempting to fetch the first asset, and set that to image. Here's the kicker... when no assets are found, image will be set to NULL.
This next part is where your problem is...
{{ image.url }}

That'll work great assuming an asset was found. But when no assets were found, this will trigger an error. Because image would be set to NULL, and NULL doesn't have an attribute of url. (This is exactly what the error message is telling you.)
Instead, you should write that line more defensively...
{{ image ? image.url }}

This technique uses the ternary operator to first check "if image is truthy" before attempting to output image.url. In other words, if image contains a valid asset, output the image URL.
That way, if no asset is found, you won't get a big nasty error message.

On a separate note... You seem to be expressing some urgency that your site has gone down. But the screenshot you showed is clearly a byproduct of being in devMode. For the record, you never want to have devMode enabled in a live production environment.


---

### Answer by username_2 on 2015-09-26. Score: 2

It's a bit hard to tell from this snippet, but if `image` is an asset field it's important to remember that these are returned as arrays so you have to access the url more like `block.image.first().url`. However, if the image variable is set elsewhere this probably won't help.

EDIT: [Relevant docs](http://buildwithcraft.com/docs/assets-fields#templating)


---

