I got the following error while trying to install a new custom module

Recoverable fatal error: Argument 2 passed to Symfony\Component\Routing\Route::__construct() must be of the type array, null given

Same error was observed while clearing cache and in drush commands. While debugging the issue I found that it is caused due to formatting issues in the .routing.yml file of my custom module.

My earlier code was a below

my_module.form:
    path: '/my-form'
    defaults:
    _title: 'My form'
    _form: '\Drupal\my_module\Form\MyForm'
    requirements:
    _permission: 'access content'

After adjusting the spacing issue in "defaults" and "requirements" the issue was solved.

Code after formatting

my_module.form:
    path: '/my-form'
    defaults:
      _title: 'My form'
      _form: '\Drupal\my_module\Form\MyForm'
    requirements:
      _permission: 'access content'

Ensure that all your code is formatted properly to fix this issue.