How can I get the uploaded file path of a contact form in drupal 8
You can get the file entity directly from the field and check if the field is not empty before applying any methods on the file object:

/** @var \Drupal\contact\Entity\Message $contactMessage */
$contactMessage = $message['params']['contact_message'];
$file = $contactMessage-&gt;field_file-&gt;entity;
if (!empty($file)) {
  $fileInfo[] = [
    'value' =&gt; $file-&gt;getFilename(),
    'url' =&gt; $file-&gt;url(),
  ];
}