I have tried to figure this out for probably 3 weeks so I decided to post it here. When I print to the stream (printwriter.println()) I get a null exception. I can just try printing a hard coded string and I still receive it. I do not see where I am either A.) Not initializing or B.) Object being null..... I went ahead and used System.out.println on the socket to make sure it wasn't null and it is infact not null. Please see the code below and offer suggestions if you have them.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import com.server.request.IncomingRequest;
public class ConnectionHandler extends Thread{
private Socket socket = null;
private IncomingRequest[] req;
public ConnectionHandler(Socket socket, IncomingRequest[] _req){
this.req = _req;
this.socket = socket;
}
public void run(){
BufferedReader bufferedReader = null;
String msg = new String();
PrintWriter printWriter;
int rep = 0;
try{
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
printWriter = new PrintWriter(socket.getOutputStream(),true);
msg = bufferedReader.readLine();
if(msg != null){
rep = Integer.parseInt(msg);
printWriter.println(req[rep].getRep());
}
printWriter.close();
bufferedReader.close();
System.runFinalization();
}catch(IOException e){
System.out.println(e);
}
}
protected void finalize() throws Throwable {
try {
socket.close();
System.gc();
} finally {
super.finalize();
}
}
}

New Topic/Question
Reply




MultiQuote








|