GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes problem reading a string from a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem reading a string from a text file" Watch "problem reading a string from a text file" New topic
Author

problem reading a string from a text file

Alonso Tapia
Greenhorn

Joined: May 12, 2011
Posts: 12
Hello all

I'm currently working on a correction over some values from a text file that contain numbers with another that contains numbers as well. So I need to read the string that contains the numbers, that are semicolon separated values, convert it to doubles and then check with the numbers from another similar file. This is my code



What is very strange is that this code works perfect sometimes, but for some reason if I run it again it crashes in line 26. The thing is that after some runs it stops reading the strings I need with the readLine method of BufferedReader and returns a null value. As the string value is null, the StringTokenizer crashes. For the first file work fine always.
I'm really stuck and don't understand what's wrong. Please help!!
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

a stacktrace would be helpful


Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
Alonso Tapia
Greenhorn

Joined: May 12, 2011
Posts: 12
oh well, I do have this rutine inside a Try catch block that handle IOExceptions and FileNotFoundExceptions. The exception I get is a NullPointerException.
In the StringTokenizer points to line 199: maxPosition = str.length(); and then to line 221 from the first constructor:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Are you telling us you are asking a question about some code which you didn't post? Please don't do that in future.

As for your NullPointerExpression, in the example you posted that's happening because the "str" variable is null. And that (assuming the code you posted is pretty close to the actual code) would be because you tried to read from some file and you were already at the end of that file. This could happen even when you try to read the first line, if the file is actually empty.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
Wait isn't arachivoOUT the output file? Wouldn't it initially be empty? So when you try to read it in line 25 it returns null which causes NullPointerException at line 26
Alonso Tapia
Greenhorn

Joined: May 12, 2011
Posts: 12
code I didn't post??? no, it's entirely mine, except the first "3" (line "3" I mean line 5; lines 3-4 are the same that 1-2) that I implemented from tutorials to read text files...

Anyway the files are never empty (I've checked). The only concept I don't understand is the end of file; if I start by opening the file, why should it reach the end inmediatly??

Alonso Tapia
Greenhorn

Joined: May 12, 2011
Posts: 12
archivoOUT IS the output file, but it's thrown from another method, this one is to make some corrections after the initial calculation.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Alonso Tapia wrote:Anyway the files are never empty (I've checked). The only concept I don't understand is the end of file; if I start by opening the file, why should it reach the end inmediatly??

If you open an empty file then you are already at the end of the file. Trying to read from it, if you're using a BufferedReader, will return a null value.

And that's what your code, and the error message, says is happening. That means you didn't check the file correctly, or at the right time. Don't reject what the code is telling you.
Alonso Tapia
Greenhorn

Joined: May 12, 2011
Posts: 12
I know it's strange, but that's what's happening, and I've checked several times only to find somthing more strange: If I debug the program with a breakpoint on line 25 or before.. IT WORKS!!! If I debug it with breakpoints after line 25 or just run it: NullPointerEception.

strange huh?? at least for me..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It probably isn't the debugging that changes things, it's the fact that you are pausing for a few seconds before trying to read the line. I predict that if you put some code before line 25 which did a Thread.sleep for 30 seconds, you would get the same effect as if you were in debug mode.

That's still strange, of course. It implies that some other code is running somewhere else which is writing to the file you are trying to read from.

I notice that you are reading from the same file you write to later in the code. But you don't close the input file before you start writing to the output file. That's a bad idea, since the two files are the same. You should always close a file as soon as you have finished using it. So my suggestion would be to close the input files before you start writing the output file.
Alonso Tapia
Greenhorn

Joined: May 12, 2011
Posts: 12
Paul: YOU'RE THE MAN!!
the problem was that the output file from the first calculation was running on the same thread so as you say, it was open when I tried to read it again. The library i'm using has the option for the original calculation to run in a separate thread, so that solved my problem.

THANKS!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem reading a string from a text file