Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File - NullPointerException

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am getting NULLPointerException when working with java IO File .

I am creating the File based on some condition , if the condition is not met , the File will be not initailized.

so please see this code :



// Please let me know , how can avoid NullPointerException in this case .

Thanks
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you test if the file is null you are not getting NullPointerException at line 10 but probably somewhere else.


Can you please post more code, i mean more code of the real class Test?

The following code is really similar to yours. As you can test by yourself, it compiles and runs fine






 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to be performing you testing in a main method, I think that is probably why you are getting the null pointer exception. Also you need to import the Java File.IO class, this enables you to create new files.

Ex:


-Hunter
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted won't even compile: as requested, please post the *actual* code that's causing a problem. And when reporting an exception, post some way of determining what code is actually causing the exception, because you won't get an NPE from the line you indicate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic