PrintWriter#println(message)
과 함께 String
메시지를 보내려고하지만 스레드를 호출하자마자 예외가 발생합니다. 이 메시지의 출처를 알 수는 없습니다. 메시지가 null
인지 확인했지만 그렇지 않습니다. 내가 스레드를 호출 할 경우 여기PrintWriter # println()이 스레드에서 NullPointerException을 throw합니다.
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 (message)'줄에? – Veger
@ 예고편 예! – user3141990