Template $page is ok in debug but null when accessing a field

Hello everyone,
I have a template file where i need to pull a specific page . So i use

$category = $site->index()->findBy('acronym', $currentPage->categorie()->value());

When i debug the $category variable, all is ok

object(Kirby\Cms\Page)#509 (15) {
  object(Kirby\Cms\Pages)#517 (0) {
  object(Kirby\Cms\Content)#602 (3) {
    string(21) "Formation courte (FC)"
    string(2) "FC"
    string(8) "chr9ax9x"
  object(Kirby\Cms\Files)#676 (0) {
  string(12) "categorie/fc"
  string(35) "//localhost:3000/media/pages/categorie/fc"
  string(37) "/application/media/pages/categorie/fc"
  string(9) "categorie"
  string(2) "fc"
  object(Kirby\Cms\Template)#678 (3) {
    string(7) "default"
    string(4) "html"
    string(4) "html"
  object(Kirby\Cms\Collection)#599 (2) {
    string(2) "en"
    string(2) "fr"
  string(2) "fc"
  string(12) "categorie/fc"
  string(23) "//localhost:3000/categorie/fc"
  object(Kirby\Cms\Pages)#507 (4) {
    string(13) "categorie/cas"
    string(13) "categorie/das"
    string(12) "categorie/fc"
    string(13) "categorie/mas"

But i if try to access a field, il got an error

// error : Call to a member function title() on null

Thanks in advance :slight_smile:

Are you doing this in a loop where not every $category will return a page object as a result.

In any case, you should never ever call a class object without having made sure in an if statement if you have an object:

if ($category = $site->index()->findBy('acronym', $currentPage->categorie()->value())) {
  echo $category->title();

Not in a loop, iā€™d like to pull only a single page which is relates to my current page but not the classic way (not accessible by page->children())

It is still best practice to use an if statement to make sure you have an object, loop or not.

I agree, but in not mandatory in this case since i 'm able to debug it

What does $currentPage->categorie()->value() return?