Repository: dita-ot/dita-ot
Issue: NullPointerException in FileUtils.getRelativePath

The stack trace:

https://www.oxygenxml.com/forum/viewtopic.php?f=20&t=20682

     D:\\Oxygen XML Author 21\\frameworks\\dita\\DITA-OT3.x\\plugins\\org.dita.base\\build_preprocess.xml:87: java.lang.NullPointerException
	at org.dita.dost.util.FileUtils.getRelativePath(FileUtils.java:169)
	at org.dita.dost.module.DebugAndFilterModule.getRelativePathFromOut(DebugAndFilterModule.java:532)
	at org.dita.dost.module.DebugAndFilterModule.getPathtoProject(DebugAndFilterModule.java:514)
	at org.dita.dost.writer.DitaWriterFilter.startDocument(DitaWriterFilter.java:95)
	at org.xml.sax.helpers.XMLFilterImpl.startDocument(Unknown Source)
	at org.dita.dost.writer.TopicFragmentFilter.startDocument(TopicFragmentFil

It looks like a side effect of a condition added by @robander in: https://github.com/dita-ot/dita-ot/pull/2797

Changed files:
src/main/java/org/dita/dost/util/FileUtils.java, src/test/java/org/dita/dost/util/TestFileUtils.java

diff --git a/src/main/java/org/dita/dost/util/FileUtils.java b/src/main/java/org/dita/dost/util/FileUtils.java
index efd70ad3c..e0544f85d 100644
--- a/src/main/java/org/dita/dost/util/FileUtils.java
+++ b/src/main/java/org/dita/dost/util/FileUtils.java
@@ -166,7 +166,7 @@ public final class FileUtils {
      * @return relative path, or refPath if different root means no relative path is possible
      */
     public static File getRelativePath(final File basePath, final File refPath) {
-        if (!basePath.toPath().getRoot().equals(refPath.toPath().getRoot())) {
+        if (basePath.toPath().getRoot() == null || !basePath.toPath().getRoot().equals(refPath.toPath().getRoot())) {
             return refPath;
         }
         return basePath.toPath().getParent().relativize(refPath.toPath()).toFile();
diff --git a/src/test/java/org/dita/dost/util/TestFileUtils.java b/src/test/java/org/dita/dost/util/TestFileUtils.java
index 666547bea..3110579ef 100644
--- a/src/test/java/org/dita/dost/util/TestFileUtils.java
+++ b/src/test/java/org/dita/dost/util/TestFileUtils.java
@@ -77,11 +77,13 @@ public class TestFileUtils {
             assertEquals(new File("d:\\\\a.dita"), FileUtils.getRelativePath(new File("c:\\\\map.ditamap"), new File("d:\\\\a.dita")));
             assertEquals(new File("a.dita"), FileUtils.getRelativePath(new File("c:\\\\map1\\\\map2\\\\map.ditamap"), new File("c:\\\\map1\\\\map2\\\\a.dita")));
             assertEquals(new File("../topic/a.dita"), FileUtils.getRelativePath(new File("c:\\\\map1\\\\map.ditamap"), new File("c:\\\\topic\\\\a.dita")));
+            assertEquals(new File("D:\\\\top_level_error\\\\out\\\\pdf-OPENME2\\\\index.html"), FileUtils.getRelativePath(new File("B1\\\\B2\\\\B3\\\\B4\\\\topic.dita"), new File("D:\\\\top_level_error\\\\out\\\\pdf-OPENME2\\\\index.html")));
         } else {
             assertEquals(new File("../a.dita"), FileUtils.getRelativePath(new File("/map/map.ditamap"), new File("/a.dita")));
             assertEquals(new File("a.dita"), FileUtils.getRelativePath(new File("/map.ditamap"), new File("/a.dita")));
             assertEquals(new File("a.dita"), FileUtils.getRelativePath(new File("/map1/map2/map.ditamap"), new File("/map1/map2/a.dita")));
             assertEquals(new File("../topic/a.dita"), FileUtils.getRelativePath(new File("/map1/map.ditamap"), new File("/topic/a.dita")));
+            assertEquals(new File("/top_level_error/out/pdf-OPENME2/index.html"), FileUtils.getRelativePath(new File("B1/B2/B3/B4/topic.dita"), new File("/top_level_error/out/pdf-OPENME2/index.html")));
         }
     }