• 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

exception in reading a text file

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I am reading a file in a loop, whichi is being written by some other thread at the same time. The other thread updates the text file. The problem is that during reading the file it throws NULLPOINTEREXCEPTION. Could the casue be that when I am trying to read the file, at the same time the file is opened by other thread for writing ?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should definitely not try to read and write a file at the same time; that's bound to cause problems.
 
Paul Wagner
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I am stuck in this situation. The file causing problems contain information, that is important and need to be displayed to the user.
Isnt there any method available in Java API, which can tell the status of the file ? I mean get around solution for this problem could be to check the status of the fle. if its open, then try next time, if not read it and close it.. m i right ?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is useless to speculate on your NullPointerException since you don't show the code in the vicinity of the statement that causes it.

Intelligent checking of references for null before using them is the general hint that applies here.

Bill
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It occurs to me that you need something like the Unix "tail" command that can be set up to write to the standard output stream as lines are written to the end of a specified file. If that was started as a Java Process you could catch the output lines whenever they are written.

Bill
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Wagner wrote:Isnt there any method available in Java API, which can tell the status of the file ? I mean get around solution for this problem could be to check the status of the fle. if its open, then try next time, if not read it and close it.. m i right ?


Do you want to check whether some other process has the file open? You probably can't do that.

Or do you want to check whether your own code has the file open? You can do that with ordinary boolean variables, or perhaps with better-organized code which doesn't need to check in the first place.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a class that I use in my own implementation of TAIL.

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ideally when you read the file you should get the last stored version of that file.
Like in windows when multiple users use a shared file, sometime it warns, ‘user A’ has open the file for writing, do you want to open it in read only mode, and you can open it in read only mode & you see last stored version.

I am not sure why Java will behave differently.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic