Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Created File Has No Parent?

In a java program, I create a file with

File temp = new File("temp");
temp.createNewFile();

Then for some reason when I write

File pDir = temp.getParentFile();

and pDir is null. I actually want to write

File pDir = temp.getParentFile().getParentFile();

but that throws a null pointer exception.

like image 614
SSEMember Avatar asked Jul 02 '12 15:07

SSEMember


1 Answers

You need a file with a path for that, try getAbsoluteFile.

File pDir = temp.getAbsoluteFile().getParentFile();
like image 138
J-16 SDiZ Avatar answered Sep 30 '22 03:09

J-16 SDiZ