Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

f.getParentFile().exists() throws java.lang.NullPointerException

hkcoderAug 26 2010 — edited Aug 26 2010
Hi,

I was just trying to test whether the Parent Path is exists for any file path or not. Here is my code snippet :
String strFPath="c:/";
String strParentFileStatus="";
File f=new File(strFPath);
if(f.exists())
{
           strParentFileStatus=(f.getParentFile().exists()) ? f.getParentFile().toString():"Parent does not exists";
           System.out.println(strParentFileStatus);
}
I got the exception at this line :
           strParentFileStatus=(f.getParentFile().exists()) ? f.getParentFile().toString():"Parent does not exists";
java.lang.NullPointerException
        at javacert50.FileDetails.getFileDetails(IOOperations.java:36)
        at javacert50.IOOperations.main(IOOperations.java:133)
According to docs, getParentFile().exists() Tests whether the file or directory denoted by this abstract pathname exists.So, I think this method returns me the true(parent exists) or false(parent not exists) value instead of NullPointerException.

Any suggestions?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 23 2010
Added on Aug 26 2010
5 comments
1,077 views