Question

I have a problem. This is my code:

File root = new File(path);
String[] entries = root.list();
LinkedList<String> out = new LinkedList<String>();
for(String entry : entries) {
 ....
}

In my code I'm trying to list contents of directory which is on C:. But this code returns me null on for loop start. Somewhere on the web I found that it returns null when file is not a directory or if I don't have administrator rights. I've tried to run Eclipse as administrator and then run my program, but I'm still getting NullPointerException and I don't know how to move. Does anybody know how can I edit my code or what to do to have it working? Please help.

Was it helpful?

Solution

Perhaps you should try calling File.isDirectory() for the root object and checking its result before trying anything else.

It could very well be that the path in question does not actually refer to a directory or it may not even exist at all...

OTHER TIPS

Why don't you just make yourself administrator and test it out.

Go to control panel User Accounts and Change your User Account Type to Admin.

You have to start your java program starting the java process as Administrator.

One way to do this is open a Console as administrator and launch your program: e.g. Start->All programs->Accessories->Command Prompt (right-click on it, "run as administrator")

Then, from this console, you can start the java process with Admin rights:

java yourpackage.YourProgram

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top