wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes why it throws NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why it throws NullPointerException" Watch "why it throws NullPointerException" New topic
Author

why it throws NullPointerException

Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
HI ranchers ,i am new to Files concept,
kindly go thru the code and let me know my mistake
thanks


import java.io.*;
class Files
{
public static void main(String args[]) throws NullPointerException
{
String dirName="c:/Test";
File f=new File(dirName,"work.txt");
File f3=new File(dirName,"renFile.txt");
System.out.println("File name is :"+f.getName());
System.out.println("Path of the file is :"+f.getPath());
System.out.println(" Parent directory is :"+f.getParent());
System.out.println("Listing the contents of directory");
File f1=new File(dirName);
String s[]=f1.list();
for(int i=0;i<s.length;i++)
{
File f2=new File("\t"+dirName+"/"+s[i]);
if(f2.isDirectory())
{
System.out.println("\t"+s[i]+"is a directory");
}
else
{
System.out.println("\t"+s[i]+"is a file");
}
}
}
}
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

Jenny,
When you run the code, it tells you what line the NullPointer is on. Can you post that (along with which line in your code that it corresponds to) ?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
File name is :work.txt
Path of the file is :c:\Test\work.txt
Parent directory is :c:\Test
Listing the contents of directory
Exception in thread "main" java.lang.NullPointerException
at Files.main(Files.java:15)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I'm not on a Windows machine, but my guess is that the problem is...

String dirName="c:/Test";

Shouldn't this use a backslash instead of a forward slash? If this isn't a valid directory, then you're going to get null with...

String s[]=f1.list();

...and then a NullPointerException when you try to call s.length.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
File f=new File(dirName,"work.txt");


what does the above line do"?

does it create a new file named work.txt or should there be a file already excisting under the name work.txt

i don't follow the code completely

help me to understand the concepts of accessing file and its properties
Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
hi marc

String dirName="c:/Test";

there is nothing wrong in the above line,i double checked

anyway i tried creating a folder named test,now it didn't throw exception,

but can u explain the code,what it is actually doing

thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jenny raj:
...i tried creating a folder named test,now it didn't throw exception...

That's the issue. There was no directory called "test" on C, and so you got null from...

String s[]=f1.list();

...and then a NullPointerException when you tried to call s.length.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The I/O chapter from Thinking in Java does a nice job of working with File objects. See if that helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why it throws NullPointerException
 
Similar Threads
list of files in a directory
Move file to new folder
IO
NoClassDefFoundError
problrm in creation of file and dircectory