# Unable to display image (Call to a member function url() on null)

**URL:** <https://forum.getkirby.com/t/unable-to-display-image-call-to-a-member-function-url-on-null/7030>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 22, 2017, 10:20am UTC](https://forum.getkirby.com/t/unable-to-display-image-call-to-a-member-function-url-on-null/7030 "2017-03-22T10:20:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andalusi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/andalusi/32/1700_2.png) [@andalusi](https://forum.getkirby.com/u/andalusi)\
**Post date:** [March 22, 2017, 10:20am UTC](https://forum.getkirby.com/t/unable-to-display-image-call-to-a-member-function-url-on-null/7030/1 "2017-03-22T10:20:47Z")

</div>

While:

```
 <?php foreach($pages->find('menu')->children() as $menu): ?>
     <?php echo $menu->title()->html() ?>
 <?php endforeach ?>

```

outputs the correct title, the following code:

```
 <?php foreach($pages->find('menu')->children() as $menu): ?>
     <img src="<?php echo $menu->image()->url() ?>" alt="" class=""/>
 <?php endforeach ?>

```

results in an error (Call to a member function url() on null).

What do I missing?  
The image is located in:

```
  content/
  content/menu
  content/menu/1-restauant
  content/menu/1-restauant/vegetables.jpg

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 22, 2017, 10:35am UTC](https://forum.getkirby.com/t/unable-to-display-image-call-to-a-member-function-url-on-null/7030/2 "2017-03-22T10:35:55Z")

</div>

Do all children have an image in it?

To avoid errors, you should **always** (always, always, always) check if an image object exists before calling a method on it:

```auto
<?php foreach($pages->find('menu')->children() as $menu): ?>
  <?php if($image = $menu->image()): ?>
     <img src="<?php echo $image->url() ?>" alt="" class=""/>
  <?php endif ?>
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![andalusi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/andalusi/32/1700_2.png) [@andalusi](https://forum.getkirby.com/u/andalusi)\
**Post date:** [March 22, 2017, 10:37am UTC](https://forum.getkirby.com/t/unable-to-display-image-call-to-a-member-function-url-on-null/7030/3 "2017-03-22T10:37:24Z")

</div>

Good point!

Thanks, it works now 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:50pm UTC](https://forum.getkirby.com/t/unable-to-display-image-call-to-a-member-function-url-on-null/7030/4 "2025-02-06T16:50:24Z")

</div>


