JDK-8133521 : (fs) Paths.get(null); throws NullPointerException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 8u51,9
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2015-08-12
  • Updated: 2016-06-13
  • Resolved: 2015-08-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9Resolved
Description
FULL PRODUCT VERSION :
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux xxx 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u1 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
java.lang.NullPointerException
at sun.nio.fs.UnixPath.normalizeAndCheck(UnixPath.java:77)
at sun.nio.fs.UnixPath.(UnixPath.java:71)
at sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:281)
at java.nio.file.Paths.get(Paths.java:84)

when using Path p = Paths.get(null);

There is no javadoc indicating the NullPointerException, and according to the javadoc it should throw "InvalidPathException - if the path string cannot be converted to a Path"

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just use Paths.get(null); on an unix system (which will use UnixPath)

I just tested other things on windows :
Paths.get(null) leads to : 
Exception in thread "main" java.lang.NullPointerException
	at java.nio.file.Paths.get(Paths.java:132)
	at atest.test.main(test.java:15)

and Paths.get((String)null); leads to :
Exception in thread "main" java.lang.NullPointerException
	at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:98)
	at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
	at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
	at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
	at java.nio.file.Paths.get(Paths.java:84)
	at atest.test.main(test.java:16)




EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect some javadocs comments or correct exceptions to be thrown (IllegalArgumentException ?)
ACTUAL -
Multiple different errors depending on the platform and the signature of Paths.get used.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public static void main(String[] args) {
		try {
			Paths.get(null);
		} catch (Exception e) {
			System.out.println("Path.get(URI) with null arg error");
			e.printStackTrace();
		}
		try {
			Paths.get((String) null);
		} catch (Exception e) {
			System.out.println("Path.get(String...varargs) with null arg error");
			e.printStackTrace();
		}
	}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Null check before using Paths.get() with "if"


Comments
Closed "Not an Issue" bugs without verification
13-06-2016

Elena is correct: the sentence that she quoted is in the "General Exceptions" section of the description of the package java.nio.file. We recommend that users not only read the documentation of the specific class or method in question, but also at least the description of the package in which the class is contained.
18-08-2015

- Run the attached test case with JDK 8u51, 8u60 ea b27 and 9 ea b76 and could reproduce the reported exception. - Output with JDK 8u51: > java PathBug Path.get(URI) with null arg error java.lang.NullPointerException at java.nio.file.Paths.get(Paths.java:132) at PathBug.main(PathBug.java:8) Path.get(String...varargs) with null arg error java.lang.NullPointerException at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:98) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94) at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255) at java.nio.file.Paths.get(Paths.java:84) at PathBug.main(PathBug.java:14)
13-08-2015

According to the package spec "Unless otherwise noted, passing a null argument to a constructor or method of any class or interface in this package will cause a NullPointerException to be thrown." So, NPE should be thrown in the described case.
13-08-2015