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) {
  ["children"]=>
  object(Kirby\Cms\Pages)#517 (0) {
  }
  ["content"]=>
  object(Kirby\Cms\Content)#602 (3) {
    ["title"]=>
    string(21) "Formation courte (FC)"
    ["acronym"]=>
    string(2) "FC"
    ["autoid"]=>
    string(8) "chr9ax9x"
  }
  ["files"]=>
  object(Kirby\Cms\Files)#676 (0) {
  }
  ["id"]=>
  string(12) "categorie/fc"
  ["mediaUrl"]=>
  string(35) "//localhost:3000/media/pages/categorie/fc"
  ["mediaRoot"]=>
  string(37) "/application/media/pages/categorie/fc"
  ["num"]=>
  NULL
  ["parent"]=>
  string(9) "categorie"
  ["slug"]=>
  string(2) "fc"
  ["template"]=>
  object(Kirby\Cms\Template)#678 (3) {
    ["name":protected]=>
    string(7) "default"
    ["type":protected]=>
    string(4) "html"
    ["defaultType":protected]=>
    string(4) "html"
  }
  ["translations"]=>
  object(Kirby\Cms\Collection)#599 (2) {
    [0]=>
    string(2) "en"
    [1]=>
    string(2) "fr"
  }
  ["uid"]=>
  string(2) "fc"
  ["uri"]=>
  string(12) "categorie/fc"
  ["url"]=>
  string(23) "//localhost:3000/categorie/fc"
  ["siblings"]=>
  object(Kirby\Cms\Pages)#507 (4) {
    [0]=>
    string(13) "categorie/cas"
    [1]=>
    string(13) "categorie/das"
    [2]=>
    string(12) "categorie/fc"
    [3]=>
    string(13) "categorie/mas"
  }
}

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

var_dump($category->title()); 
// 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?