diff --git a/extensions/twig/CHANGELOG.md b/extensions/twig/CHANGELOG.md
index 7e498c7..9fecc97 100644
--- a/extensions/twig/CHANGELOG.md
+++ b/extensions/twig/CHANGELOG.md
@@ -7,6 +7,7 @@ Yii Framework 2 twig extension Change Log
 - Bug #2925: Fixed throwing exception when accessing AR property with null value (samdark)
 - Bug #3767: Fixed repeated adding of extensions when using config. One may now pass extension instances as well (grachov)
 - Bug #3877: Fixed `lexerOptions` throwing exception (dapatrese)
+- Bug #4290: Fixed throwing exception when trying to access AR relation that is null (samdark, tenitski)
 - Enh #1799: Added `form_begin`, `form_end` to twig extension (samdark)
 - Enh #3674: Various enhancements (samdark)
     - Removed `FileLoader` and used `\Twig_Loader_Filesystem` instead.
diff --git a/extensions/twig/Extension.php b/extensions/twig/Extension.php
index 2062a51..0680782 100644
--- a/extensions/twig/Extension.php
+++ b/extensions/twig/Extension.php
@@ -54,11 +54,6 @@ class Extension extends \Twig_Extension
         ];
     }
 
-    public function initRuntime(\Twig_Environment $environment)
-    {
-        $environment->setBaseTemplateClass('yii\twig\Template');
-    }
-
     /**
      * @inheritdoc
      */
diff --git a/extensions/twig/Template.php b/extensions/twig/Template.php
index 19a4a49..4d0103d 100755
--- a/extensions/twig/Template.php
+++ b/extensions/twig/Template.php
@@ -14,12 +14,14 @@ namespace yii\twig;
  */
 abstract class Template extends \Twig_Template
 {
-    protected function getAttribute($object, $item, array $arguments = array(), $type = \Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
+    /**
+     * @inheritdoc
+     */
+    protected function getAttribute($object, $item, array $arguments = [], $type = \Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
     {
         // Twig uses isset() to check if attribute exists which does not work when attribute exists but is null
-
         if ($object instanceof \yii\db\BaseActiveRecord) {
-            if ($type == \Twig_Template::METHOD_CALL) {
+            if ($type === \Twig_Template::METHOD_CALL) {
                 return $object->$item();
             } else {
                 return $object->$item;
diff --git a/extensions/twig/ViewRenderer.php b/extensions/twig/ViewRenderer.php
index c9ccbe2..6c5fdf8 100644
--- a/extensions/twig/ViewRenderer.php
+++ b/extensions/twig/ViewRenderer.php
@@ -98,6 +98,8 @@ class ViewRenderer extends BaseViewRenderer
             'charset' => Yii::$app->charset,
         ], $this->options));
 
+        $this->twig->setBaseTemplateClass('yii\twig\Template');
+
         // Adding custom globals (objects or static classes)
         if (!empty($this->globals)) {
             $this->addGlobals($this->globals);