Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Weird NullPointerException inside BufferedReader.readLine()

Thread view: 
John English - 04 Nov 2005 10:30 GMT
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:je@brighton.ac.uk
 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
-----------------------------------------------------------------
Roedy Green - 04 Nov 2005 10:47 GMT
>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?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Uppal - 04 Nov 2005 13:07 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.