URGENT! java.lang.NullPointerException with File object

why would this give me java.lang.NullPointerException ?

File directory = new File("C:/Temp");
File[] fileList = directory.listFiles();

is there something else I should look for in the code?
Who is Participating?
dttriConnect With a Mentor Commented:
  Because your code is on Windows platform, you may change:
File directory = new File("C:/Temp");
File directory = new File("C:\\Temp");
  This may resolve the problem.

polkadotAuthor Commented:
btw, I get the error in run time, not compile time ...
91mustangConnect With a Mentor Commented:
I will give you the error if the Directory C:/Temp does not exist
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

C:/Temp will work fine (as long as the dir exists), I just tested it.
bcsonkaConnect With a Mentor Commented:
Are you sure the NullPointerException is occurring on one of those lines?  Also, is it possible that C:/Temp is not a directory on the system?  

If C:/Temp is not a directory on the system, listFiles() will return null and accessing fileList at that point will throw a NullPointerException.

I hope that helps.
polkadotAuthor Commented:

C:/Temp is a directory ... and it works that way ... so I'll give you all good grades :)

but not this way:

public class A{

File directory;

public A(File d)
{  directory = d; }

public void M()
{ File[] fileList = directory.listFiles();}

can you explain why, that when I want it to remeber the directory in the method, It forgets ?

polkadotAuthor Commented:
>>This may resolve the problem.

It won't

>>can you explain why, that when I want it to remeber the directory in the method, It forgets ?

Maybe - if you post the rest of the code for class A
 You should post your solution, not just comment CEHJ.
>>You should post your solution

As i mentioned, polkadot will have to post more code
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.