How do I programmatically render a custom entity? I created a custom entity (submenu) in Drupal 8. I need to render this custom entity in my custom module. This is the code I am using. 

$entity_type = 'submenu';
$entity_id = '1'; // static for example purpose
$view_mode = 'full';         

$entity = \Drupal::entityTypeManager()-&gt;getStorage($entity_type)-&gt;load($entity_id);
$view_builder = \Drupal::entityTypeManager()-&gt;getViewBuilder('submenu');
$pre_render = $view_builder-&gt;view($entity, $view_mode);
$render_output = render($pre_render);


I keep getting the following error.


  TypeError: Argument 1 passed to Drupal\Core\Entity\EntityViewBuilder::view() must implement interface Drupal\Core\Entity\EntityInterface, null given


Why do I get this error?
Make sure the entity id exists and the error disappears.