NullPointerException in PrintWriter.flush()

Hi,

I have this code:

protectedvoid sendDoc( Element el ){

try{

Source sou =new DOMSource( el );

Writer str =new StringWriter();

Result output =new StreamResult( str );

PrintWriter printOut =new PrintWriter( out,true );

TransformerFactory tf = TransformerFactory.newInstance();

Transformer tr = tf.newTransformer();

tr.transform( sou, output );

printOut.println( str );

System.out.println( str );

}catch( Exception e ){

System.out.println( e );

}

}

that I use to send an XML document (generated correctly elsewhere).

The problem is that I use this code many times and after many times working correct (something like 26) I got a NullPointerException exception at the printOut.println(str) line. Going deep with debugging I am able to see that the problem is in the flush() function and exactly when out.flush is performed in PrintWriter.

out is obviously initialized and in this case is a Socket.getOutputStream.

If it could help:

m.shuffle();

for(int j=0; j<13; j++ ){

for(int i=0; i<players.size(); i++ ){

((Giocatore)players.elementAt(i)).addCarta(m.pesca());

}

}

while(true ){

GiocatoreServer current = (GiocatoreServer)players.elementAt( turn );

turn++;

if( turn == players.size() ) turn = 0;

current.playTurn( m.pesca() );

}

this is where the problem occours: before the while(true) the program is working fine but when it enters the while loop it hangs as said (the function playTurn simply contains two calls that builds the XML and then call sendDoc).

Any idea is very welcome, I can't understand this exception (maybe I have to do some re-initialization to PrintWriter?).

Another point: if I make PrintWriter not autoflushing and then I change the println() with print() and the flush() the code doesn't work at all...>

[2833 byte] By [vltavaa] at [2007-10-1 23:57:24]
# 1

Wait a minute. You're getting a NullPointerException here:

printOut.println( str );

where printOut is defined here:

Writer str = new StringWriter();

Result output = new StreamResult( str );

PrintWriter printOut = new PrintWriter( out, true );

So you're sending the StringWriter as output to itself?

Why are you doing that...? Or have I missed something?

paulcwa at 2007-7-15 15:49:59 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Forget it. Brain ****. out != output
paulcwa at 2007-7-15 15:49:59 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3
But what is "out" then?
paulcwa at 2007-7-15 15:49:59 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4
out is an OutputWriter taken from a Socket (via getOutputWriter())
vltavaa at 2007-7-15 15:49:59 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5
And you said that in the original post as well. Yesterday was a bad day for me.Perhaps the socket was closed, although I'd expect a different exception from that.It might be interesting to try to test that behavior, to see if you can reproduce the error with a known cause.
paulcwa at 2007-7-15 15:49:59 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

No problem for that 'out' thing...

No, it's not a socket problem: if I close the socket (i.e. I close one of the clients connected to the server that run the code above) I get another exception (I can't remember right now its name but I'm sure it's different and I catch that exception with a try/catch block and output something like 'socket closed' or 'socket terminated').

About the settings that leads to the NullPointer Exception, I can only repeat it like mentioned before, after 13*2 repetitions...

It sounds really strange and maybe I could try to search deeper for an aswer to that strange number (maybe reducing the loop counter or similar). I'll let you know if I get some results.

Luca

vltavaa at 2007-7-15 15:49:59 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...