diff --git a/src/Element/ComponentElementAlter.php b/src/Element/ComponentElementAlter.php
index 13a8c58bbbb7d556c96e4335cf2fe6be28b600ee..fddd816b86f7bed4ff69ea889a23dcc1218f646b 100644
--- a/src/Element/ComponentElementAlter.php
+++ b/src/Element/ComponentElementAlter.php
@@ -84,7 +84,7 @@ class ComponentElementAlter implements TrustedCallbackInterface {
     // merging with it.
     $element["#props"]["attributes"] = NestedArray::mergeDeep(
       $element["#attributes"],
-      $element["#props"]["attributes"]
+      $element["#props"]["attributes"] ?? []
     );
     return $element;
   }
diff --git a/src/Template/TwigExtension.php b/src/Template/TwigExtension.php
index d18a2ed676228646135955e5eb53995c39ece6e5..b9cdf10a84cebed4edf581d288dd4961e21fb4e3 100644
--- a/src/Template/TwigExtension.php
+++ b/src/Template/TwigExtension.php
@@ -145,6 +145,7 @@ class TwigExtension extends AbstractExtension {
     // an Attribute PHP object before running the validator.
     // Attribute PHP object are casted as string by the validator and trigger
     // '[attributes] String value found, but an object is required' error.
+    $context['attributes'] = $context['attributes'] ?? [];
     $context['attributes']['data-component-id'] = $component->getPluginId();
   }