diff --git a/hist/histpainter/src/TGraph2DPainter.cxx b/hist/histpainter/src/TGraph2DPainter.cxx
index bb7e9d12ecca07fa6126d5f2f9305586263c0d4b..322ba1035a6f1c1e503fd8e21d43ec2580217111 100644
--- a/hist/histpainter/src/TGraph2DPainter.cxx
+++ b/hist/histpainter/src/TGraph2DPainter.cxx
@@ -171,7 +171,12 @@ TList *TGraph2DPainter::GetContourList(Double_t contour)
       if (fZ[p1]>z2)  {z2=fZ[p1]; x2=fX[p1]; y2=fY[p1]; i2=1;}
       if (fZ[p2]<=z0) {z0=fZ[p2]; x0=fX[p2]; y0=fY[p2]; i0=2;}
       if (fZ[p2]>z2)  {z2=fZ[p2]; x2=fX[p2]; y2=fY[p2]; i2=2;}
-      i1 = 3-i2-i0;
+      if (i0==0 && i2==0) {
+         Error("GetContourList", "wrong vertices ordering");
+         return 0;
+      } else {
+         i1 = 3-i2-i0;
+      }
       x1 = fX[t[i1]-1];
       y1 = fY[t[i1]-1];
       z1 = fZ[t[i1]-1];