"Call to a member function toFile() on null" exclusively for images on home page

Hey there!

I’m having some issues with displaying the images on the home page of my Kirby 3 site.

In all my templates, I’m calling single images like this:

<img src="<?= $page->myImage()->toFile()->url() ?>"/>

In the fields section of all the blueprints, image fields are configured as follows:

myImage:
 label: Image
 type: files
 template: image
 multiple: false

This works perfectly fine on all pages except the home page.
Using the same method, I’m getting a “Call to a member function toFile() on null” error message for all my images there.

I already confirmed that the correct filenames are indeed stored in the home.txt file:

  myImage:
    -image.jpg

All other contents are properly rendered, it’s just with the images on the home page that I’m getting this error.

First of all, it should be myImage(), note the parenthesis after myImage

Secondly, never call the URL (or any other class method) method without an if-statement to make sure you got an object.

<?php if($image = $page->myImage()->toFile()): ?>
<img src="<?= $image->url() ?>"/>
<?php endif ?>

Please do not use quotes, but wrap code blocks in three backticks on a separate line before and after the code block to enable syntax highlighting.

2 Likes

Hey there!

Thanks for the quick reply! Sorry about the quote; first post on the forums! ; )

I edited in the missing parenthesis right after posting, but you were so quick to reply that you didn’t see that anymore. I had just skipped them in my post and they are in the template, of course - so that wasn’t the issue.

However, your other suggestion did the trick. Thanks!
Might I ask why though? I realize that an if-statement to ensure you’re dealing with an object before using an object function is good practice, but that still leaves me puzzled as to why the images on the home page of my site do not work if I drop that statement while all the others do.

If the file exists, it works without the if statement as well and I can’t tell you from here why it didn’t.

Hm, that’s curious then; I can’t confirm that.

Even if I don’t make use of the $myImage variable inside the if-statement (just for testing), this works:

<?php if($myImage = $page->myImage()->toFile()): ?>
          <img src="<?= $page->myImage()->toFile()->url() ?>" alt="<?= $myImage->alt() ?>"/>
<?php endif ?>

…while the same file call without the statement does not work:

<img src="<?= $page->myImage()->toFile()->url() ?>" alt="<?= $myImage->alt() ?>"/>

No other changes were involved; content files remain unchanged and the file exists. And as pointed out, this happens exclusively on the home page of the site.

Ah well, let’s not waste any more of your time in any case; no reason not to have the if-statement anyway.
Thanks again! ; )

Still weird, I’ll test this on a home page tomorrow, maybe it’s some sort of weird bug after all.:sleeping:

Probably unrelated, @Weltverloren, but I noticed that the code for your alt property is different in the two examples.

One uses alt="<?= $myImage->alt() ?>",
and the other uses alt="<?= $introImage->alt() ?>".

Is $introImage defined?

Yeah, I changed the actual variable/file names to “myImage” for the sake of the example and missed one. Thanks for pointing it out; edited my post. ; )