diff --git a/layout/html/table/src/nsTableFrame.cpp b/layout/html/table/src/nsTableFrame.cpp
index f77c6dcebfec..9647c300ccbc 100644
--- a/layout/html/table/src/nsTableFrame.cpp
+++ b/layout/html/table/src/nsTableFrame.cpp
@@ -4320,7 +4320,7 @@ nsTableFrame::GetFrameAtOrBefore(nsIPresContext* aPresContext,
nsIFrame* childFrame;
nsIFrame* lastMatchingFrame = nsnull;
aParentFrame->FirstChild(aPresContext, nsnull, &childFrame);
- while (childFrame != aPriorChildFrame) {
+ while (childFrame && childFrame != aPriorChildFrame) {
childFrame->GetFrameType(&frameType);
if (aChildType == frameType) {
lastMatchingFrame = childFrame;
diff --git a/layout/tables/nsTableFrame.cpp b/layout/tables/nsTableFrame.cpp
index f77c6dcebfec..9647c300ccbc 100644
--- a/layout/tables/nsTableFrame.cpp
+++ b/layout/tables/nsTableFrame.cpp
@@ -4320,7 +4320,7 @@ nsTableFrame::GetFrameAtOrBefore(nsIPresContext* aPresContext,
nsIFrame* childFrame;
nsIFrame* lastMatchingFrame = nsnull;
aParentFrame->FirstChild(aPresContext, nsnull, &childFrame);
- while (childFrame != aPriorChildFrame) {
+ while (childFrame && childFrame != aPriorChildFrame) {
childFrame->GetFrameType(&frameType);
if (aChildType == frameType) {
lastMatchingFrame = childFrame;