Solved

URGENT! java.lang.NullPointerException with File object

Posted on 2004-09-18
10
203 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
Comment Utility
btw, I get the error in run time, not compile time ...
0
 
LVL 7

Accepted Solution

by:
dttri earned 166 total points
Comment Utility
 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 167 total points
Comment Utility
I will give you the error if the Directory C:/Temp does not exist
0
 
LVL 4

Expert Comment

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

Assisted Solution

by:bcsonka
bcsonka earned 167 total points
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:polkadot
Comment Utility
:/

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
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
 You should post your solution, not just comment CEHJ.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>You should post your solution

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 75
changeXy challenge 13 56
Java array passed to SQL where clause 23 39
Java Timer (static) 9 10
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now