Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

URGENT! java.lang.NullPointerException with File object

Posted on 2004-09-18
10
Medium Priority
?
209 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 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

705 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