Impossible to invoke a method ("getUrl") on a null variable 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 :)
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