Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

URGENT! java.lang.NullPointerException with File object

Posted on 2004-09-18
10
Medium Priority
?
210 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:polkadot
  • 3
  • 2
  • 2
  • +2
10 Comments
 

Author Comment

by:polkadot
ID: 12094054
btw, I get the error in run time, not compile time ...
0
 
LVL 7

Accepted Solution

by:
dttri earned 664 total points
ID: 12094105
 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
 
LVL 4

Assisted Solution

by:91mustang
91mustang earned 668 total points
ID: 12094112
I will give you the error if the Directory C:/Temp does not exist
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:91mustang
ID: 12094117
C:/Temp will work fine (as long as the dir exists), I just tested it.
0
 
LVL 2

Assisted Solution

by:bcsonka
bcsonka earned 668 total points
ID: 12094127
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
 

Author Comment

by:polkadot
ID: 12094196
:/

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
 

Author Comment

by:polkadot
ID: 12094215
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12094876
>>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
 
LVL 7

Expert Comment

by:dttri
ID: 12095158
 You should post your solution, not just comment CEHJ.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12095194
>>You should post your solution

As i mentioned, polkadot will have to post more code
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question