Welcome to Dream.In.Code
Become a Java Expert!

Join 150,194 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,034 people online right now. Registration is fast and FREE... Join Now!




Problem with BufferedReader - NullPointerException

 
Reply to this topicStart new topic

Problem with BufferedReader - NullPointerException

prelic
16 Sep, 2008 - 06:26 PM
Post #1

New D.I.C Head
*

Joined: 13 Feb, 2007
Posts: 15


My Contributions
Hey, so basically I have a file that is multiple lines long, different entries for different people, pretty simple, basically looks like:

Name1: Entry, Entry2
Name2: Entry, Entry2
Name3: Entry, Entry2

Simple enough, but whenever I run it, it prints out the second line, then throws a NullPointerException, and I have no idea why, my guess is it has something to do with my while loop, but idk...heres my code:
CODE
Poker pokerGame = new Poker();
        int hand_rank;
        File in = new File(
                "/Users/philrelic/Documents/workspace/PokerHand/src/Hands.txt");
        BufferedReader reader = new BufferedReader(new FileReader(in));
        while ((reader.readLine()) != null)
        {
            String line = reader.readLine();
            hand_rank = Poker.ParseHand(line, pokerGame);
        }


Any help is appreciated, thanks guys
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Problem With BufferedReader - NullPointerException
16 Sep, 2008 - 06:36 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The problem is that your code is calling readLine twice. Once in the while loop and then again inside the while loop. Remember that each time you call readLine you have to store the value because it will pull the line right out of the stream and advance the file pointer. So what is happening is that you are advancing the pointer in the while loop condition and then reading it again in the loop. Well if the while loop reaches the end of file in the while condition, when you call it in the loop you are going to get your null exception because there is nothing left to read.

Try something along this line for your while loop...

java

String line = "";
while ((line = reader.readLine()) != null)
{
hand_rank = Poker.ParseHand(line, pokerGame);
}


Here we are reading the line from the file directly into the line variable and that is being compared to null. If all is ok, then we go ahead and use the line in the loop.

Notice we have only one read per iteration of the loop and when we reach the end of the file, the while loop will be null and terminate. The way you had it the while loop would read the last line, enter the loop, then try to read again.

Hope this makes sense.

"At DIC we be file line storing code ninjas... we also store away money, arms, food and an occasional woman. Ok I lied about the woman because we can't get a date if our lives depended on it." decap.gif


User is offlineProfile CardPM
+Quote Post

ritab68
RE: Problem With BufferedReader - NullPointerException
30 Oct, 2008 - 12:58 PM
Post #3

New D.I.C Head
*

Joined: 6 Oct, 2007
Posts: 14


My Contributions
QUOTE(Martyr2 @ 16 Sep, 2008 - 07:36 PM) *

The problem is that your code is calling readLine twice. Once in the while loop and then again inside the while loop. Remember that each time you call readLine you have to store the value because it will pull the line right out of the stream and advance the file pointer. So what is happening is that you are advancing the pointer in the while loop condition and then reading it again in the loop. Well if the while loop reaches the end of file in the while condition, when you call it in the loop you are going to get your null exception because there is nothing left to read.

Try something along this line for your while loop...

java

String line = "";
while ((line = reader.readLine()) != null)
{
hand_rank = Poker.ParseHand(line, pokerGame);
}


Here we are reading the line from the file directly into the line variable and that is being compared to null. If all is ok, then we go ahead and use the line in the loop.

Notice we have only one read per iteration of the loop and when we reach the end of the file, the while loop will be null and terminate. The way you had it the while loop would read the last line, enter the loop, then try to read again.

Hope this makes sense.

"At DIC we be file line storing code ninjas... we also store away money, arms, food and an occasional woman. Ok I lied about the woman because we can't get a date if our lives depended on it." decap.gif


Was helpful to me!!! biggrin.gif

Thanks a bunch,
Rita.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:23AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month