2012-02-24 5 views
0

nullpointer 예외가 계속 발생하지만 문제가 어디인지 알 수 없습니다. 도와주세요. android에있는 클라이언트의 자바 코드입니다. 동시에 데이터를 보내고 받으려고하지만 작동하지 않습니다. <null 포인터 예외를 수정할 수 없습니다.

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); 
       } 


} 

} 당신이 BufferWriter 함께 초기화 out.println("CLOSE");

+1

디버거를 사용하십시오. –

+1

여기에 오류 로그를 입력하십시오. plz. –

+3

stack trace please – L7ColWinters

답변

0

에서 변수 ... 그 그래서 당신은 점점 NullPointerException이 밖으로 를 초기화해야

+0

내가 그랬어. 하지만 여전히 작동하지 않습니다. 빈 화면 만 표시합니다. 레이아웃 없음. – rockernerd

+0

위의 줄을 디버그하고'socket'에있는 내용을 확인하십시오. – Ved

3

.

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

실제로 그것을 읽는 소품. – L7ColWinters

+0

@ L7ColWinters .. 누군가가 null 포인터 예외를 말하고 많은 클래스 변수를 가질 가능성이 가장 높습니다. 초기화되지 않았습니다. – ngesh

+0

예 .. 감사합니다! dat d 문제 .. bt 작동하지 않습니다. – rockernerd

관련 문제