diff --git a/src/debug.c b/src/debug.c
index a73df9abc92c409a978cdb197877ee842759fad3..7376e4558b48fa0414cf1df02d4a7c1de400d1d9 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -200,7 +200,9 @@ void dumpMETISGraph(const char *prefix, idx_t nvertices, idx_t nvertexweights,
   for (i = 0; i < nvertices; i++) {
     fprintf(stdfile, "\n");
     fprintf(simplefile, "\n");
-    fprintf(weightfile, "\n");
+    if (weightfile != NULL) {
+        fprintf(weightfile, "\n");
+    }
 
     if (havevertexsize) {
       fprintf(stdfile, " %" PRIDX, vertexsizes[i]);
@@ -225,7 +227,9 @@ void dumpMETISGraph(const char *prefix, idx_t nvertices, idx_t nvertexweights,
   }
   fprintf(stdfile, "\n");
   fprintf(simplefile, "\n");
-  fprintf(weightfile, "\n");
+  if (weightfile != NULL) {
+      fprintf(weightfile, "\n");
+  }
 
   fclose(stdfile);
   fclose(simplefile);