Solved

URGENT! java.lang.NullPointerException with File object

Posted on 2004-09-18
10
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

634 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