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...>

