当前位置: 动力学知识库 > 问答 > 编程问答 >

NullPointerException Socket - Android?

问题描述:

I am trying to use from this LINK.

Here is client code :

public class MainActivity extends AppCompatActivity {

private Socket socket;

private static final int SERVERPORT = 5001;

private static final String SERVER_IP = "127.0.0.1";

Button myButton;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

myButton = (Button)findViewById(R.id.myButton);

myButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

try {

EditText et = (EditText) findViewById(R.id.EditText01);

String str = et.getText().toString();

PrintWriter out = new PrintWriter(new BufferedWriter(

new OutputStreamWriter(socket.getOutputStream())),

true);

out.println(str);

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

});

new Thread(new ClientThread()).start();

}

class ClientThread implements Runnable {

@Override

public void run() {

try {

InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

socket = new Socket(serverAddr, SERVERPORT);

} catch (UnknownHostException e1) {

e1.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

And here server code :

 public class MainActivity extends AppCompatActivity {

private ServerSocket serverSocket;

Handler updateConversationHandler;

Thread serverThread = null;

private TextView text;

public static final int SERVERPORT = 6001;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.text2);

updateConversationHandler = new Handler();

this.serverThread = new Thread(new ServerThread());

this.serverThread.start();

}

@Override

protected void onStop() {

super.onStop();

try {

serverSocket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

class ServerThread implements Runnable {

public void run() {

Socket socket = null;

try {

serverSocket = new ServerSocket(SERVERPORT);

} catch (IOException e) {

e.printStackTrace();

}

while (!Thread.currentThread().isInterrupted()) {

try {

socket = serverSocket.accept();

CommunicationThread commThread = new CommunicationThread(socket);

new Thread(commThread).start();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

class CommunicationThread implements Runnable {

private Socket clientSocket;

private BufferedReader input;

public CommunicationThread(Socket clientSocket) {

this.clientSocket = clientSocket;

try {

this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));

} catch (IOException e) {

e.printStackTrace();

}

}

public void run() {

while (!Thread.currentThread().isInterrupted()) {

try {

String read = input.readLine();

updateConversationHandler.post(new updateUIThread(read));

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

class updateUIThread implements Runnable {

private String msg;

public updateUIThread(String str) {

this.msg = str;

}

@Override

public void run() {

text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");

}

}

}

Get me bellow error and I can't see any message on server :

网友答案:

In your Client code, you haven't created the client-socket, but, you're trying to get the output stream of that socket using socket.getOutputStream(). Since socket is null, hence, your code throws NullPointerException.

So, create a socket object for client to connect to Server as shown below :

public class MainActivity extends AppCompatActivity {

private Socket socket;

private static final int SERVERPORT = 5001;
private static final String SERVER_IP = "127.0.0.1";
Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myButton = (Button)findViewById(R.id.myButton);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                EditText et = (EditText) findViewById(R.id.EditText01);
                String str = et.getText().toString();
//create socket object as shown below before retrieving the OutputStream object.
                socket = new Socket(MainActivity.SERVER_IP,MainActivity.SERVERPORT);
                PrintWriter out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket.getOutputStream())),
                        true);
                out.println(str);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    new Thread(new ClientThread()).start();
}
//.... and your code goes on!
分享给朋友:
您可能感兴趣的文章:
随机阅读: