• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



This code is giving null Pointer Exception
Can anyone please help me how to remove that exception???
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if filesInDirectory is null before iterating over it. File.listFiles can return null; the Javadoc page tells you when this may occur. All you need to do is check if it did.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerExceptions are usually easy to fix. Look at the stack trace. It will tell you exactly in which line the exception happens.

Look at that line of your source code and check what could be null there that should not be null.

Note: You get a NullPointerException when you try to call a method on a variable that is null.

From the code that you posted it's impossible to tell where exactly and why the NullPointerException happens.
 
karan chandok
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parseAllFiles(f.getPath());

for(File f : filesInDirectory)



Got null point exception in these two lines

Any suggestion please
 
Rob Spoor
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is my previous post invisible or something? I told you exactly where the problem is. And I can also tell you why this occurs - because you're trying to list the files in a File that's not a folder but a file itself. You never make the distinction. But even if you do, listFiles will still return null if the folder cannot be accessed.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karan chandok wrote:parseAllFiles(f.getPath());

for(File f : filesInDirectory)



Got null point exception in these two lines

Any suggestion please


you might have got that because your parent directory may be empty...incase if the parent directory is not empty please tell about the contents in parent directory...!!!
 
Ranch Hand
Posts: 297
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you can find the source of NullPointerException by using Exception break point in Eclipse which will tell you at which point exactly your code throws NPE and can also suspend the thread for you to inspect.

you may find my post about debugging in eclipse interesting. 10 tips on debugging java program

ignore it if you already know how to do this stuff.
 
Rob Spoor
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Kumaar wrote:you might have got that because your parent directory may be empty...incase if the parent directory is not empty please tell about the contents in parent directory...!!!

Empty directories will result in empty arrays, not null.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!