Weird NullPointerException inside BufferedReader.readLine()

J

John English

I'm getting a very weird NullPointerException from deep in the bowels
of BufferedReader.readLine(), and having looked at the 1.5 source for
the library I'm not the wiser how this can happen -- the library code
for BufferedInputStream.read() (the actual source of the exception)
only seems to manipulate two ints (pos & count) and call two other
methods.

What I'm doing is: I'm using exec() to create a new Process, then using
getInputStream() to get the input stream from the Process. So far so
good -- the process is created, I get the input stream. I wrap it in
a BufferedReader:
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
This also seems fine.

I then call in.readLine() and the following happens:

java.lang.NullPointerException
at java.io.BufferedInputStream.read(BufferedInputStream.java:279)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:408)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:450)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:182)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at foo.TestNode.runTest(TestNode.java:125)
(... and so on ...)

TestNode.java line 125 is the call to in.readLine(), btw.

I've tried this with 1.5 and 1.4.2 (both on a RedHat Linux system) and
it fails identically on both.

Any got *any* ideas at all what's happening here?

-----------------------------------------------------------------
John English | mailto:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
 
R

Roedy Green

java.lang.NullPointerException
at java.io.BufferedInputStream.read(BufferedInputStream.java:279)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:408)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:450)

We don't have code to experiment. Have a look at the stream you are
reading. It is truly encoded with the encoding you told the Reader?

It SHOULD not crap out, but perhaps some sort of malformed stream
could cause it to.


BuffereredInputStream in JDK 1.5.0_05 line 279 is in the middle of a
comment. Could you look it up in your JDK in src.zip to show the code
surrounding line 279.


By any chance did you do a:

public synchronized int read(byte b[], int off, int len)

where b is null?
 
C

Chris Uppal

John said:
I'm getting a very weird NullPointerException from deep in the bowels
of BufferedReader.readLine(), and having looked at the 1.5 source for
the library

BTW, the line numbers you quoted correspond to the 1.4 source, not the 1.5. I
wouldn't mention it except that if you are running 1.4 code when you think you
are running 1.5 then you may have a deeper problem.

I'm not the wiser how this can happen -- the library code
for BufferedInputStream.read() (the actual source of the exception)
only seems to manipulate two ints (pos & count) and call two other
methods.

This is something of a long-shot, and rather vague at that...
As far as I can see the only way you can get a NullPointerException there is
if another thread has closed the BufferedInputStream between the call to
ensureOpen() at the start of BufferedInputStream.read() and the execution of
in.available() (which is the only thing that could /possibly/ throw a NPE).
Since it's normal to use threads when reading/writing sub-processes
stdout/stdin, it may be that you can find your problem by looking at what the
other threads are doing.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top