Answers.Body: <p>Well, the <a href="https://craftcms.com/docs/templating/elementcriteriamodel#first" rel="noreferrer"><code>first</code></a> method <em>only tries</em> to get an asset model. But if there's no asset added to the field it simply returns <code>null</code>. So calling a method like <code>getUrl</code>, or accessing a property like <code>url</code> or <code>title</code> can't work, as they do not exist for <code>null</code>.</p>
  
  <pre><code>{% set image = entry.geschaeftsbereichHeadImage.first() %}
  
  {% if image %}
      &lt;img src="{{ image.url(headBanner) }}"&gt;
  {% endif %}
  </code></pre>
  
  <p>Or if you'd like to display a fallback image consider somthing like this:</p>
  
  <pre><code>{% set image = entry.geschaeftsbereichHeadImage.first() %}
  {% set imageUrl = image ? image.url(headBanner) : 'path/to/fallback.jpg' %}
  
  &lt;img src="{{ imageUrl }}"&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-27T12:23:08.903Z
Answers.LastActivityDate: 2016-01-27T13:59:03.577Z
Answers.LastEditDate: 2016-01-27T13:59:03.577Z
Answers.UserDisplayName: carlcs
Body: <p>I call a asset like this:</p>
  
  <pre><code>{% set headImage = entry.geschaeftsbereichHeadImage.first() %}
  &lt;img src="{{ headImage.url(headBanner) }}"
  </code></pre>
  
  <p>If I don't ad any image to this field there is a twig error:</p>
  
  <blockquote>
    <p>Impossible to invoke a method ("url") on a null variable</p>
  </blockquote>
  
  <p>When I don't ad any Image to this field in the CP shouldn't there be just no image? Why the error? If I ad a image it works just fine.</p>
  
Tags: <templating><assets><error-message>
Title: Impossible to invoke a method on a null variable
ThreadText: # Impossible to invoke a method on a null variable
  
  (Asked by: username_0 on 2016-01-27)
  
  I call a asset like this:
  
  ```
  {% set headImage = entry.geschaeftsbereichHeadImage.first() %}
  <img src="{{ headImage.url(headBanner) }}"
  
  ```
  
  If I don't ad any image to this field there is a twig error:
  
  > Impossible to invoke a method ("url") on a null variable
  
  When I don't ad any Image to this field in the CP shouldn't there be just no image? Why the error? If I ad a image it works just fine.
  
  **Tags:** templating, assets, error-message
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-01-27. Score: 6 | Accepted answer
  
  Well, the [`first`](https://craftcms.com/docs/templating/elementcriteriamodel#first) method *only tries* to get an asset model. But if there's no asset added to the field it simply returns `null`. So calling a method like `getUrl`, or accessing a property like `url` or `title` can't work, as they do not exist for `null`.
  
  ```
  {% set image = entry.geschaeftsbereichHeadImage.first() %}
  
  {% if image %}
      <img src="{{ image.url(headBanner) }}">
  {% endif %}
  
  ```
  
  Or if you'd like to display a fallback image consider somthing like this:
  
  ```
  {% set image = entry.geschaeftsbereichHeadImage.first() %}
  {% set imageUrl = image ? image.url(headBanner) : 'path/to/fallback.jpg' %}
  
  <img src="{{ imageUrl }}">
  
  ```
  
  
  ---
  
  