• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why it throws NullPointerException

 
Jenny raj
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ?
 
Jenny raj
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jenny raj
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The I/O chapter from Thinking in Java does a nice job of working with File objects. See if that helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic