uri – NOTE: Attempting to use property & # 39; nid & # 39; to retrieve a non-object

In Drupal rsvplist Form RSVPForm-> buildForm () I wrote the following to get the current NID:

$node = Drupal::routeMatch()->getParameter('node');
$nid = $node->nid->value;

I get this error:

Note: An attempt is made to use the property & # 39; nid & # 39; of a non-object in retrieve
Drupal rsvplist Form RSVPForm-> buildForm () (line 30 of
modules custom rsvplist src Form RSVPForm.php).

I am a new module developer. How do I get the current NID in Drupal 8?