2014-01-08 54 views
0

我試圖用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(); 
    } 

    } 
+0

'out'是否非null? – laalto

+0

NPE在哪裏拋出?在'out.println(消息)'行? – Veger

+0

@Veger是的! – user3141990

回答

0

我會後這是一個答案,因爲它是有點長了一個註釋(即使我不能提供一個完整的/直接的解決方案)。發生

該問題的原因:

  1. outClientDataService尚未設定,當你在IncomingHandler
  2. 創建了對象,以便mservice.getOutputstream()回報null
  3. 您在Send_Client使用。

通過使用new ClientDataService()直接創建衍生對象,您正在濫用ServerIntent

相反,你需要按以下方式啓動一個服務,acording到Android Service documentation

Intent intent = new Intent(this, HelloService.class); 
startService(intent); 

所以我猜你需要(這看起來對我很重要的文件中大量的信息),瞭解更多關於Android服務以便在您的應用程序中正確使用它們。

+0

我想我找到了問題,是的,事實上,我試過這個解決方案,因爲當單獨使用時,它返回null。 由於傳入的處理程序是ClientDataService的內部類,我只需將ClientDataService.this.out並通常它應該工作,而不使用任何新的ClientDataService()的實例。非常感謝您的幫助@Veger – user3141990

相關問題