diff --git a/core/lib/Drupal/Core/Template/TwigTemplate.php b/core/lib/Drupal/Core/Template/TwigTemplate.php
index ee864ca..b2a92ab 100644
--- a/core/lib/Drupal/Core/Template/TwigTemplate.php
+++ b/core/lib/Drupal/Core/Template/TwigTemplate.php
@@ -41,8 +41,9 @@
    */
   final protected function getContextReference(&$context, $item)
   {
-    // Optimized version
-    if (!isset($context[$item])) {
+    // Optimized version. NULL is a valid value for $context[$item], we only
+    // want to error if it hasn't been defined at all.
+    if (!isset($context[$item]) && @($context[$item] !== NULL)) {
       // We don't want to throw an exception, but issue a warning instead.
       // This is the easiest way to do so.
       // @todo Decide based on prod vs. dev setting
diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php
index c62f7c3..1ed6cee 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Theme/ThemeTestTwig.php
@@ -69,4 +69,15 @@ function testFindThemeTemplates() {
     $templates = drupal_find_theme_templates($cache, '.html.twig', drupal_get_path('theme', 'test_theme_twig'));
     $this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.html.twig was found in test_theme_twig.');
   }
+
+  /**
+   * Tests that Twig templates handle PHP various data types correctly.
+   */
+  function testTwigVariableDataTypes() {
+    config('system.theme')
+      ->set('default', 'test_theme_twig')
+      ->save();
+    $this->drupalGet('theme-test/template-test');
+    $this->assertRaw('<p id="null-test">null: </p>', t('NULL values render as empty strings in Twig templates.'));
+  }
 }
diff --git a/core/modules/system/tests/themes/test_theme_twig/theme_test.template_test.html.twig b/core/modules/system/tests/themes/test_theme_twig/theme_test.template_test.html.twig
index 6cda319..d8957ec 100644
--- a/core/modules/system/tests/themes/test_theme_twig/theme_test.template_test.html.twig
+++ b/core/modules/system/tests/themes/test_theme_twig/theme_test.template_test.html.twig
@@ -1,2 +1,4 @@
 {# Output for Theme API test #}
 Success: Template overridden.
+
+<p id="null-test">null: {{ twig_null }}</p>
