Don't Run PHP if Object is NULL

On the homepage, I have code that is pulling from a subpage of a subpage. When the URL of that sub-subpage is changed, it then causes the homepage to throw a null error because the homepage can no longer find the associated page object. The code currently looks like this:

  <figure class="main-carousel" id="<?= $page->uid() ?>">
    <?php $projects = $center->hpfeatured()->toStructure();
     foreach ($projects as $project): ?>
        <?php $selectproject = $project->hpoption()->toPage() ?>
        <?php if($selectproject->featured()->isNotEmpty()): ?>
          <?php $episode = $selectproject->featured()->toPage() ?>
          <div class="carousel-item" id="<?= $selectproject->uid() ?>">
           <?php snippet('post-type', ['page' => $episode])?></div>
        <?php endif ?>
    <?php endforeach ?>
    </figure>

How would I make it so that if the object is NULL, it just shows up empty as opposed to throwing a NULL error?

There are actually two bits that can go wrong and need an if statement each:

 <?php $selectproject = $project->hpoption()->toPage() ?>

And

  <?php $episode = $selectproject->featured()->toPage() ?>

Fix:

<?php $projects = $center->hpfeatured()->toStructure(); ?>
<?php if ( $projects->isNotEmpty() ) : ?>
 <figure class="main-carousel" id="<?= $page->uid() ?>">
     <?php foreach ($projects as $project): ?>
        <?php if ( $selectproject = $project->hpoption()->toPage() ) : ?>
           <?php if ( $episode =  $selectproject->featured()->toPage() ) : ?>
              <div class="carousel-item" id="<?= $selectproject->uid() ?>">
           <?php snippet('post-type', ['page' => $episode])?></div>
        <?php endif ?>
       <?php endif; ?>
    <?php endforeach ?>
    </figure>
<?php endif; ?>

The isNotEmpty() if statement is no longer necessary in this case, because there will be no page if the field is empty.

I’ve also changed the order a bit, because you probably want to check if there are any projects, to prevent an empty figure tag if not.

Thanks this works though would just like to state that there is a > missing after:
<?php if ( $episode = $selectproject->featured()->toPage() ) : ?