2012-02-24 86 views
0

我連續得到一個空指針異常,但我無法弄清楚問題出在哪裏。 請幫忙。 它是Android上的客戶端的java代碼。我試圖同時發送和接收數據,但它不工作。 <空指針異常無法修復

package com.chat.clientone; 
import android.app.Activity; 
import android.os.Bundle; 
import java.io.*; 
import java.net.*; 

import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnKeyListener; 
import android.view.KeyEvent; 
import android.widget.*; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
    import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
    import java.net.Socket; 
import java.net.UnknownHostException; 


import android.text.Html; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.EditText; 
import android.util.*; 

public class ClientoneActivity extends Activity { 

public static final String SERVER_HOSTNAME = "10.0.2.2"; 
public static final int SERVER_PORT = 3000; 
private TextView tv=null; 
private EditText et=null; 
private Button bt=null; 
private Socket socket; 
BufferedReader in=null; 
PrintWriter out=null; 
String messageOut=null, messageIn=null; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);     
    tv=(TextView)findViewById(R.id.clienttext); 
    et=(EditText)findViewById(R.id.clientedit); 
    bt=(Button)findViewById(R.id.cbtn); 


    try {   
      socket = new Socket(SERVER_HOSTNAME, SERVER_PORT); 

      tv.append("Connected to server " + 
        SERVER_HOSTNAME + ":" + SERVER_PORT); 
      et.append("Enter Username"); 

     }catch (UnknownHostException e1) { 
     e1.printStackTrace(); 
     } catch (IOException e1) { 
     e1.printStackTrace(); 
     } 


      try { 
       in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
       tv.append("NOTE: Enter 'quit' to end the program.\n"); 
       while (true) { 
        //System.out.print("SEND:  "); 
        messageOut = et.getText().toString()+System.getProperty("line.separator"); 
        Log.e("LOG_TAG", "ERRRRRRRROOORRR"+messageOut); 
        if (messageOut.equalsIgnoreCase("quit")) { 
          // User wants to quit. Inform the other side 
          // of the connection, then close the connection. 
         out.println("CLOSE"); 
         out.flush(); 
        // connection.close(); 
         tv.append("Connection closed."); 
        // finish(); 
         System.exit(0); 
         break; 
        } 
        out.println(messageOut); 
        out.flush(); 
        if (out.checkError()) { 
         throw new IOException("Error occurred while transmitting message."); 
        } 
        tv.append("WAITING..."); 
        messageIn = in.readLine(); 
        if (messageIn.length() > 0) { 
          // The first character of the message is a command. If 
          // the command is CLOSE, then the connection is closed. 
          // Otherwise, remove the command character from the 
          // message and procede. 
         if (messageIn.equalsIgnoreCase("CLOSE")) { 
          tv.append("Connection closed at other end."); 
         // connection.close(); 
        //  finish(); 
          System.exit(0); 
          break; 
         } 

        } 
        tv.append("RECEIVED: " + messageIn); 
       } 
       } 
       catch (Exception e) { 
       tv.append("Sorry, an error has occurred. Connection lost."); 
       tv.append(e.toString()); 
       System.out.println(e); 
       Log.e("LOG_TAG", "ERRRRRRRROOORRR"+e.toString()); 
       System.exit(1); 
       } 


} 

}

+1

使用調試器。 – 2012-02-24 06:02:36

+1

把你的錯誤日誌在這裏PLZ。 – 2012-02-24 06:02:38

+3

堆棧跟蹤請求 – L7ColWinters 2012-02-24 06:03:04

回答

0

初始化了與BufferWriter。

out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); 
+0

我做到了。但它仍然不起作用。它只顯示空白屏幕。沒有佈局。 – rockernerd 2012-02-24 06:43:07

+0

調試上面的行,看看'socket'中有什麼 – Ved 2012-02-24 06:45:17

3

那裏有你初始化變量...它所以你得到的NullPointerException在out.println("CLOSE");

+2

實際閱讀它的道具。 – L7ColWinters 2012-02-24 06:10:27

+0

@ L7ColWinters ..當有人說空指針異常,並有很多類變量最有可能,他們沒有初始化.. – ngesh 2012-02-24 06:15:51

+0

是的..謝謝! dats d問題.. Bt它不工作。 – rockernerd 2012-02-24 06:25:52