File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Buffered reader.readLine() null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Buffered reader.readLine() null pointer exception" Watch "Buffered reader.readLine() null pointer exception" New topic
Author

Buffered reader.readLine() null pointer exception

J Marsh
Greenhorn

Joined: Dec 02, 2012
Posts: 2
Hi everyone!

I'm getting a null pointer exception when using a buffered file reader, and I can't seem to figure out why.

Here's the code that's giving me a null pointer exception:

It creates a new Buffered File reader using the File() constructor on a sample file, and tries to split each line of the file on semicolons.
The exception is on line 24, I've tried simply printing out the reader and it is not null, so I'm pretty clueless...

Here is the sample file:
Does anyone have any ideas?

Thanks a lot!

Arthur
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42266
    
  64
For starters, you should avoid giving your classes names that clash wth class names in the JRE - that's bound to cause confusion and conflicts (java.io.File).

On line 24, two objects could be null - "reader" or "reader.readLine()". It seems that "reader" should not be null -or you would have gotten an exception in line 7- so it would seem that readLine returns null. Which makes sense for a file that contains only a single line - which is consumed in line 15.

In other words, you're reading the file in line 15 but you're throwing away what you read.


Ping & DNS - my free Android networking tools app
J Marsh
Greenhorn

Joined: Dec 02, 2012
Posts: 2
Thanks a lot!
I hadn't realized that checking if the line was null actually consumed that line, removing that statement and just checking if the line is null using ready() did that trick!

I'll change the name of my class too, hadn't thought it might already exist in java..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Buffered reader.readLine() null pointer exception