我試圖用PrintWriter#println(message)
發送String
消息,但是隻要我調用線程就會拋出一個異常。 我不知道這封郵件是從哪裏來的。我檢查消息是否是null
但事實並非如此。PrintWriter#println()在線程中拋出一個NullPointerException異常
import java.io.PrintWriter;
import android.util.Log;
public class Send_Client implements Runnable {
private PrintWriter out;
private String message=null;
public Send_Client(PrintWriter out,String message) {
this.out=out;
this.message=message;
}
@Override
public void run() {
// the message is different from null
// Problem to resolve
out.println(message);
out.flush();
Log.d(MainActivity.TAG, "The message has been sent from Send_Client");
}
}
而這裏就是我所說的線程
public class ClientDataService extends IntentService {
private static Socket socket=null;
private static final int SOCKET_TIMEOUT = 5000;
public static final String ACTION_CLIENT="Action_Client";
public static final String EXTRAS_GROUP_OWNER_PORT = "go_port";
public static final String EXTRAS_GROUP_OWNER_ADDRESS = "go_host";
String message=null;
private BufferedReader in=null;
public PrintWriter out=null;
/** Command to the service to receive a message */
static final int MSG_CLIENT = 2;
public ClientDataService(String name) {
super(name);
}
public ClientDataService() {
super("ClientDataService");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent.getAction().equals(ACTION_CLIENT)) {
int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
String host= intent.getExtras().getString(EXTRAS_GROUP_OWNER_ADDRESS);
try{
Log.d(MainActivity.TAG, "ask for connection to the server");
socket=new Socket();
socket.bind(null);
socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
Log.d(MainActivity.TAG, "connection socket has been established");
try{
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out= new PrintWriter(socket.getOutputStream());
Thread t1=new Thread(new Receive_Client(in));
t1.start();
}catch(IOException e){
Log.d(MainActivity.TAG, "the client disconnect");
}
}catch (UnknownHostException e) {
Log.d(MainActivity.TAG, "impossible to connect to the host"+socket.getLocalSocketAddress());
}catch (IOException e) {
Log.d(MainActivity.TAG, "No Server Listening the port"+socket.getLocalPort());
}
}
}
public PrintWriter getOutputstream(){
return out;
}
/**
* Handler of incoming messages from UI Thread.
*/
class IncomingHandler extends Handler {
ClientDataService mservice;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_CLIENT:
mservice=new ClientDataService();
Log.d(MainActivity.TAG,"CDS has received the following message to send to the server"+(String)msg.obj);
Log.d(MainActivity.TAG, "Get the Out value"+mservice.getOutputstream().toString());
Thread t2= new Thread(new Send_Client(mservice.getOutputstream(),(String)msg.obj));
t2.start();
break;
default:
super.handleMessage(msg);
}
}
}
/**
* Target we publish for clients to send messages to IncomingHandler.
*/
final Messenger mMessenger = new Messenger(new IncomingHandler());
/**
* When binding to the service, we return an interface to our messenger
* for sending messages to the service.
*/
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(getApplicationContext(), "binding client", Toast.LENGTH_SHORT).show();
return mMessenger.getBinder();
}
}
'out'是否非null? – laalto
NPE在哪裏拋出?在'out.println(消息)'行? – Veger
@Veger是的! – user3141990