目前碰到的问题是:Android中连接Socket没问题,但是在传输数据时PrintWriter时,Java报错,java.lang.NullException。我经过测试感觉好像OuputStream实例化成PrintWriter后为Null,请问我应该如何书写Android程序,谢谢各位大师。Android源程序如下:// 通讯协议
package org.ydadling.surfaceview;public class protocol {
public char JYCode ;
public char JYLength ;
public char[] Buffer = new char[0x0ffff] ;
}// 通讯类
package org.ydadling.surfaceview;import java.net.Socket ;
import java.io.*;
import org.ydadling.surfaceview.protocol;
import  java.io.PrintWriter ;public class ClientSocket {
private Socket client=null ;

public ClientSocket(String Address,int Port) throws Exception{
client = new Socket(Address,Port) ;
}

public void SendToHost(protocol aSend) throws Exception{
if(client!=null){
if(client.isConnected()){

PrintWriter os = new PrintWriter(client.getOutputStream()) ;
aSend.JYCode = (byte) 1 ;
aSend.JYLength = (byte) 2 ;
os.write(aSend.Buffer, 0, aSend.JYLength) ;//执行到此处肯定报错
os.flush() ;
}
}
}

public void ReceiveFromHost(protocol aReceive) throws Exception{

if(client!=null){
if(client.isConnected()){
InputStream aStream  = client.getInputStream();
}
}
}

public void CloseSocket() throws Exception{
if(client!=null){
if(client.isConnected()){
client.close() ;
}
}
}

}// Activity主程序
package org.ydadling.surfaceview;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button ;
import  android.widget.TextView ;
import android.view.*;
import org.ydadling.surfaceview.protocol ;
import org.ydadling.surfaceview.ClientSocket ;
import  android.os.StrictMode ;public class TestSocket extends Activity {
private Button btnCreate,btnSend,btnEmpty=null ;
private TextView txtProtocol=null ;
private ClientSocket aSocket ;
private protocol aSend ;

class BtnCreateClick implements View.OnClickListener{
public void onClick(View paramView){
try{
aSocket = new ClientSocket("192.168.1.5",8091) ;

txtProtocol.setText("已连接服务器") ;
}
catch(Exception E){
txtProtocol.setText(E.toString()) ;
}


}
}

class BtnSendClick implements View.OnClickListener{
public void onClick(View paramView){
try{
aSend.JYCode = (byte) 1 ;
aSend.JYLength = (byte) 2 ;
aSocket.SendToHost(aSend) ;
}
catch(Exception E){
txtProtocol.setText(E.toString()) ;
}
}
}

class BtnEmptyClick implements View.OnClickListener{
public void onClick(View paramView){
txtProtocol.setText("Hello ydadling.") ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_socket);
this.btnCreate = (Button) super.findViewById(R.id.btnCreate) ;
this.btnSend = (Button) super.findViewById(R.id.btnSend) ;
this.btnEmpty = (Button) super.findViewById(R.id.btnEmpty) ;
this.txtProtocol = (TextView) super.findViewById(R.id.txtProtocol) ;
BtnSendClick OnbtnSendClick = new BtnSendClick() ;
BtnCreateClick OnbtnCreateClick = new BtnCreateClick() ;
BtnEmptyClick OnbtnEmptyClick = new BtnEmptyClick() ;
this.btnCreate.setOnClickListener(OnbtnCreateClick) ;
this.btnSend.setOnClickListener(OnbtnSendClick) ;
this.btnEmpty.setOnClickListener(OnbtnEmptyClick) ;
}
}Delphi的TServerSocket程序如下:
unit ufrmMain;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ScktComp, ExtCtrls, Grids;type
  TProtocol = Record
    JYCode: Shortint ;
    JYLength: ShortInt ;
    Buffer: Array[0..65535] of ShortInt ;
  end ;  TForm1 = class(TForm)
    ServerSocket1: TServerSocket;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ServerSocket1ClientWrite(Sender: TObject;
      Socket: TCustomWinSocket);
  private
    { Private declarations }
    aProtocol: TProtocol ;
  public
    { Public declarations }
  end;
var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if ServerSocket1.Active then
    ServerSocket1.Close ;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  with ServerSocket1 do
  begin
    Close ;
    ServerType := stThreadBlocking ;
    Port := 8091 ;
    Open ;
  end;
end;procedure TForm1.ServerSocket1ClientWrite(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  Socket.ReceiveBuf(aProtocol,Socket.ReceiveLength) ;
  {
    经过各种业务处理后
  }
  Socket.SendBuf(aProtocol,sizeOf(aProtocol)) ;
end;end.