Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Buffered reader.readLine() null pointer exception

 
J Marsh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
J Marsh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic