diff --git a/html_to_text.inc b/html_to_text.inc
index c3a9063..2fe4227 100644
--- a/html_to_text.inc
+++ b/html_to_text.inc
@@ -515,7 +515,7 @@ function _mailsystem_html_to_text_table(DOMNode $node, $allowed_tags = NULL, arr
         continue 2;
       }
       $current = $current->parentNode;
-    } while (!$current->isSameNode($node));
+    } while ($current && !$current->isSameNode($node));
     break;
   }
   // Merge the thead, tbody, and tfoot sections together.
