<p>My guess is that you need to change this:</p>

<pre><code>{% if siteBgBlureFallback %}
    background-image: url({{ siteBgBlure.first().getUrl('bodyBg') }});
{% endif %}
</code></pre>

<p>To this:</p>

<pre><code>{% if siteBgBlureFallback %}
    background-image: url({{ siteBgBlureFallback .first().getUrl('bodyBg') }});
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-12-20T11:20:21.630Z
Ups, that was a copy error from my side.

That don't work's. That is not the problem i'm looking for.
The Frog
CC BY-SA 3.0
2016-12-19T18:48:40.970Z
2016-12-19T18:48:40.970Z
Brad Bell
<p>You can do it like this:</p>

<pre><code>.bg{
    background-image: {{ entry.siteBg.first().getUrl('bodyBg')|default(siteBgFallback.siteBg.first().getUrl('bodyBg)}}
}
</code></pre>

<p>And the same for the other one. What you are doing is the following:</p>

<ol>
<li>Set an item that can be empty (hence the |default). </li>
<li>If the item is empty go to the fallback (the part between the default brackets).</li>
</ol>

<p>For more info on the default filter check <a href="http://twig.sensiolabs.org/doc/2.x/filters/default.html" rel="nofollow noreferrer">here</a></p>

CC BY-SA 3.0
2017-01-19T21:04:57.250Z
I get scared whenever I see a `first().foo`, especially when assets are involved. As soon as the file is missing you will get an error, because `first()` returns `null`.
carlcs
CC BY-SA 3.0
2017-01-19T21:33:51.077Z
I understand your concerns, however this example is only for demonstrative purposes. You could put an extra default layer to the above statement just for safety or make the fallback required in the CMS.
Jan_dh
CC BY-SA 3.0
2017-01-19T21:45:10.920Z
The file can be deleted, even if the asset field is marked as required.
carlcs
CC BY-SA 3.0
2017-01-19T18:13:48.207Z
2017-01-19T18:13:48.207Z
Jan_dh
<p>I've a problem with this code. I get the error message:</p>

<blockquote>
  <p>Impossible to invoke a method ("getUrl") on a null variable.</p>
</blockquote>

<p>For this code:</p>

<pre><code>{% if siteBgBlureFallback %}
    background-image: url({{ siteBgBlure.first().getUrl('bodyBg') }});
{% endif %}
</code></pre>

<p>Here is the full code:</p>

<pre><code>{% if entry is defined %}
    {% set siteBg = entry.siteBg %}
    {% set siteBgBlure = entry.siteBgBlure %}
{% endif %}

{% set siteBgFallback = siteBgFallback.siteBg %}
{% set siteBgBlureFallback = siteBgFallback.siteBgBlure %}

.bg {
{% if siteBg is defined and siteBg is not empty %}
    {% if siteBg %}
        background-image: url({{ siteBg.first().getUrl('bodyBg') }});
    {% endif %}

{% else %}

    {% if siteBgFallback %}
        background-image: url({{ siteBgFallback.first().getUrl('bodyBg') }});
    {% endif %}
{% endif %}
}

.bgblure {
{% if siteBgBlure is defined and siteBgBlure is not empty %}
    {% if siteBgBlure %}
        background-image: url({{ siteBgBlure.first().getUrl('bodyBg') }});
    {% endif %}

{% else %}
    {% if siteBgBlureFallback %}
        background-image: url({{ siteBgBlureFallback.first().getUrl('bodyBg') }});
    {% endif %}
{% endif %}
}
</code></pre>

<p>I don't understand why the first three outputs work and the 4th won't work any more.</p>

<p>The backup fields have some pic in there.</p>

<p>Thx for your help :)</p>

<assets>
Impossible to invoke a method ("getUrl") on a null variable
# Impossible to invoke a method ("getUrl") on a null variable

(Asked by: username_0 on 2016-12-19)

I've a problem with this code. I get the error message:

Impossible to invoke a method ("getUrl") on a null variable.

For this code:
{% if siteBgBlureFallback %}
    background-image: url({{ siteBgBlure.first().getUrl('bodyBg') }});
{% endif %}

Here is the full code:
{% if entry is defined %}
    {% set siteBg = entry.siteBg %}
    {% set siteBgBlure = entry.siteBgBlure %}
{% endif %}

{% set siteBgFallback = siteBgFallback.siteBg %}
{% set siteBgBlureFallback = siteBgFallback.siteBgBlure %}

.bg {
{% if siteBg is defined and siteBg is not empty %}
    {% if siteBg %}
        background-image: url({{ siteBg.first().getUrl('bodyBg') }});
    {% endif %}

{% else %}

    {% if siteBgFallback %}
        background-image: url({{ siteBgFallback.first().getUrl('bodyBg') }});
    {% endif %}
{% endif %}
}

.bgblure {
{% if siteBgBlure is defined and siteBgBlure is not empty %}
    {% if siteBgBlure %}
        background-image: url({{ siteBgBlure.first().getUrl('bodyBg') }});
    {% endif %}

{% else %}
    {% if siteBgBlureFallback %}
        background-image: url({{ siteBgBlureFallback.first().getUrl('bodyBg') }});
    {% endif %}
{% endif %}
}

I don't understand why the first three outputs work and the 4th won't work any more.
The backup fields have some pic in there.
Thx for your help :)

**Tags:** assets

---

## 2 answers

### Answer by username_2 on 2017-01-19. Score: 4 | Accepted answer

You can do it like this:
.bg{
    background-image: {{ entry.siteBg.first().getUrl('bodyBg')|default(siteBgFallback.siteBg.first().getUrl('bodyBg)}}
}

And the same for the other one. What you are doing is the following:

Set an item that can be empty (hence the |default). 
If the item is empty go to the fallback (the part between the default brackets).

For more info on the default filter check here

> **Comments:**
> **username_3**: I get scared whenever I see a `first().foo`, especially when assets are involved. As soon as the file is missing you will get an error, because `first()` returns `null`.  
> **username_2**: I understand your concerns, however this example is only for demonstrative purposes. You could put an extra default layer to the above statement just for safety or make the fallback required in the CMS.  
> **username_3**: The file can be deleted, even if the asset field is marked as required.  

---

### Answer by username_1 on 2016-12-19. Score: 0

My guess is that you need to change this:
{% if siteBgBlureFallback %}
    background-image: url({{ siteBgBlure.first().getUrl('bodyBg') }});
{% endif %}

To this:
{% if siteBgBlureFallback %}
    background-image: url({{ siteBgBlureFallback .first().getUrl('bodyBg') }});
{% endif %}

> **Comments:**
> **username_0**: Ups, that was a copy error from my side.  That don't work's. That is not the problem i'm looking for.  

---

