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

File - NullPointerException

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


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 .


Save India From Corruption - Anna Hazare.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
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

Joined: Mar 13, 2009
Posts: 492

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.



"If the facts don't fit the theory, get new facts" --Albert Einstein
David Newton

Joined: Sep 29, 2008
Posts: 12617

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.
I agree. Here's the link: http://aspose.com/file-tools
subject: File - NullPointerException
jQuery in Action, 3rd edition