0

I am trying to use the getParent method of the File library. Here is the code I am working with, but I am getting a null string:

public class MyCode {
    public static void main (String[] args) {
    File f = new File("");
    String name = f.getParent();
    System.out.println(name);
    }
}

This code prints "null" to the terminal. I am very confused.

2
Contributors
3
Replies
16
Views
6 Months
Discussion Span
Last Post by JamesCherrill
1

Your File object does not refer to any actual file (the file name is a zero length string) so it has not got a parent.

0

That is correct, but when I do an f.getAbsolutePath();, I get the current directory. Should I be doing something like:

File f = new File(System.getProperties("user.dir"));

0

That depends on what you are trying to achieve!
If you want the current user directory then Sytem.getProperty is the right answer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.