Path.resolve as a static method ?

Emmanuel Castro emmanuel.castro at laposte.net
Thu Dec 9 13:20:02 PST 2010


I am often tempted to use the following pattern :
  Path path=Paths.get("dir1/a")
  path.getParent().resolve("b")
It returns dir1/a
A problem occurs when path=Paths.get("a")
I await "b", but I obviously get an happy NullPointerException

I have to encapsulate my call to resolve() into a static method:
static Path safeResolve(Path mainPath, Path other) {
   if (mainPath == null) return other;
   else mainPath.resolve(other);
}

and a variant for safeResolve(Path mainPath, String other).

I think that this method should be standard, maybe in the Paths class.

Your advice?

Emmanuel CASTRO


More information about the nio-discuss mailing list