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?
polkadotAsked:
Who is Participating?
 
dttriConnect With a Mentor Commented:
 Hello,
  Because your code is on Windows platform, you may change:
File directory = new File("C:/Temp");
  to
File directory = new File("C:\\Temp");
  This may resolve the problem.

  Tri
0
 
polkadotAuthor Commented:
btw, I get the error in run time, not compile time ...
0
 
91mustangConnect With a Mentor Commented:
I will give you the error if the Directory C:/Temp does not exist
0
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.

 
91mustangCommented:
C:/Temp will work fine (as long as the dir exists), I just tested it.
0
 
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.
0
 
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 ?


0
 
polkadotAuthor Commented:
0
 
CEHJCommented:
>>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
0
 
dttriCommented:
 You should post your solution, not just comment CEHJ.
0
 
CEHJCommented:
>>You should post your solution

As i mentioned, polkadot will have to post more code
0
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.