I'm attempting to send a message to a server using sockets. I'm using a Runnable and getting the following error:
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream java.net.Socket.getOutputStream()' on a null object reference
W/System.err: at XXX.FullscreenActivity.attemptConnect(FullscreenActivity.java:387)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
W/System.err: at android.view.View.performClick(View.java:5198)
W/System.err: at android.view.View$PerformClick.run(View.java:21147)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: ERROR: Attempt to invoke virtual method 'java.io.OutputStream java.net.Socket.getOutputStream()' on a null object reference
This is the line I'm getting the error on:
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(clientsocket.getOutputStream())),
true);
The full code of the connection code is:
EditText editTextAddress = (EditText) findViewById(R.id.address);
EditText editTextPort = (EditText) findViewById(R.id.port);
SERVER_IP_loc = editTextAddress.getText().toString();
SERVERPORT_loc = Integer.parseInt(editTextPort.getText().toString());
System.out.println("AttemptConnect: IP " + SERVER_IP_loc +
" Port: " + SERVERPORT_loc);
Thread myTask = new Thread(new ClientThread(SERVER_IP_loc, SERVERPORT_loc));
myTask.start();
final String message = "CONNECT";
try{
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(clientsocket.getOutputStream())),
true);
}
catch (Exception e) {
e.printStackTrace();
System.err.println("ERROR: " + e.getMessage());
}
The socket is apparently null, but I can't figure out why. I'm starting the socket before attempting to send the message. The ClientThread code is a runnable:
class ClientThread implements Runnable {
ClientThread(String addr, int port) {
System.out.println("IN CLIENT THREAD");
SERVER_IP = addr;
SERVERPORT = port;
testMainThread(8);
}
@Override
public void run() {
testMainThread(9);
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
System.out.println("RUN2");
if(Looper.getMainLooper().getThread()==Thread.currentThread()) {
System.out.println("in main thread, don't make socket.");
}
else {
System.out.println("not in main thread, make socket.");
try {
clientsocket = new Socket(serverAddr, SERVERPORT);
}catch (Exception e3) {
e3.printStackTrace();
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}