• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in using readLine in file IO.

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here are pieces of codes I used to get input from a file then print it out line by line. I got problem when I edit my property.txt file. If the last line is a empty line, I got null pointer running time error. Can someone tell me where the problem is?
Thanks
Chris


[This message has been edited by Cindy Glass (edited August 29, 2001).]
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,
I get the inverse of what you get ie
NullPointerException when no blank lines present at the EOF &
No exception when the file has a blank line at the end .

Blank line or no lines present the problems crops from the while loop -
When EOF is encountered line is set to null .
At this point we are still trying to run a method on line(which has now become a null refernce) with the following expression
line=(theinput.readLine().trim())
This causes the NullPointerException.
Correct me if I'm wrong.
Ashish
[This message has been edited by Ashish Hareet (edited August 28, 2001).]
 
Chris Ben
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ashish,
I agree with you about the reason. But if I need to print the file like this, how can I edit my property.txt without worrying about this problem?
I can use exception handling to deal with it, but I dislike it. I remember there is something called eof in C/C++, so I can do something like while(...!=eof). How can I deal with it in java?
Thanks
Chris
P.s. I am attaching the complete testing codes

(edited by Cindy to format code)
[This message has been edited by Cindy Glass (edited August 29, 2001).]
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>Hi Chris,
From what I gather you wanna print your property.txt file as is , minus any
blank lines present at the end of the file. You could try this

while((line = theinput.readLine()) != null){
if ( !line.equals(""))
{
System.out.println(++j+" : "+line.trim());
}

No NullPointerException is generated cause the moment line becomes null the
while loop is terimated .There is a potential for IOException though .
line becomes null only when the end of stream has been reached
which in this case would mean EOF .
So I beleive you are already doin ... != EOF in the while expression
Ashish
</pre>
[This message has been edited by Ashish Hareet (edited August 29, 2001).]
 
Chris Ben
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, This program still has the nullpointer problem, which really makes me confused. I thought I would have caught the null pointer problem, but actually the problem persists.
Any suggestions?
Chris
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,
You aren't catching the NullPointerException in the code you posted . Besides you are makin the potential for a NullPointer yourself(no offence) .
Wouldn't it be better if you check for null in the while loop & then trim() in your println() ?
If you look at my code you'll see that the logic followed is yours with the only exception being that it is broken down to check for various conditions at different points .
Correct me if I'm wrong
 
Chris Ben
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thanks a lot and have great holiday
Chris
 
Chris Ben
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
great holiday
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic