diff --git a/core/lib/Drupal/Core/Routing/UrlGenerator.php b/core/lib/Drupal/Core/Routing/UrlGenerator.php
index e41fe77d40..835bf0c438 100644
--- a/core/lib/Drupal/Core/Routing/UrlGenerator.php
+++ b/core/lib/Drupal/Core/Routing/UrlGenerator.php
@@ -306,12 +306,12 @@ public function generateFromRoute($name, $parameters = [], $options = [], $colle
     }
     // Ensure the resulting path has at most one leading slash, to prevent it
     // becoming an external URL without a protocol like //example.com.
-    if (str_starts_with($path, '//')) {
+    if ($path && str_starts_with($path, '//')) {
       $path = '/' . ltrim($path, '/');
     }
     // The contexts base URL is already encoded
     // (see Symfony\Component\HttpFoundation\Request).
-    $path = str_replace($this->decodedChars[0], $this->decodedChars[1], rawurlencode($path));
+    $path = str_replace($this->decodedChars[0], $this->decodedChars[1], rawurlencode($path ?? ''));
 
     // Drupal paths rarely include dots, so skip this processing if possible.
     if (str_contains($path, '/.')) {
diff --git a/core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php b/core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php
index e9e572ca5c..6517ef48cd 100644
--- a/core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php
+++ b/core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php
@@ -51,7 +51,7 @@ public function processOutbound($path, &$options = [], Request $request = NULL,
       // also, to protect against this problem in arbitrary path processors,
       // but it is duplicated here to protect any other URL generation code
       // that might call this method separately.
-      if (str_starts_with($path, '//')) {
+      if ($path && str_starts_with($path, '//')) {
         $path = '/' . ltrim($path, '/');
       }
     }
