This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File - NullPointerException  RSS feed

 
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
 
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






 
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
 
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
Boost this thread!