Solved

URGENT! java.lang.NullPointerException with File object

Posted on 2004-09-18
10
206 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 166 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 167 total points
ID: 12094112
I will give you the error if the Directory C:/Temp does not exist
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 167 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 72
Opening PDF on button click and fill new document 2 47
Error in @AspectJ Based AOP with Spring 2 18
JavaFX TableView not displaying correctly 3 63
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

821 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