//程序在这里报错了;
this.ois = new ObjectInputStream(this.socket.getInputStream());
程序报错说空指针异常,表面上看是下面的第二条语句in在调用方法时出错的,实际上是Client.getInstance().getClientInputThread()返回了空对象,也即是ClientInputThread构造方法中的
this.ois = new ObjectInputStream(this.socket.getInputStream());
未能成功创建ois对象
这在后面打印出来的log信息也可以看出来。打印信息显示ois == null是 true,而oos == null 是 false;
事故语句块之语句1 final ClientInputThread in = Client.getInstance().getClientInputThread();
事故语句块之语句2 in.setMessageListener(new MsgListener(...){});
//输入线程
public class ClientInputThread extends Thread
{
@SuppressWarnings("unused")
private Socket socket;
private ObjectInputStream ois;
private TransEntity transEn;
private MsgListener msgListener;
private boolean isStart = true;
public ClientInputThread(Socket socket)
{
this.socket = socket;
try
{
this.ois = new ObjectInputStream(this.socket.getInputStream());
}
catch (StreamCorruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public TransEntity getMsg()
{
if(transEn!=null)
{
synchronized (this)
{
try
{
wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return transEn;
}
return null;
}
public void setMessageListener(MsgListener msgListener)
{
this.msgListener = msgListener;
}
public void releaseResource()
{
try
{
ois.close();
socket.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run()
{
while(isStart)
{
//if(ois == null)
System.out.println("ois == null : "+(ois == null));
try
{
transEn = (TransEntity) ois.readObject();
if(transEn!=null)
{
synchronized (this)
{
notify();
}
}
// msgListener.Message(transEn);
}
catch (OptionalDataException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
releaseResource();
}
}
//输出线程
public class OutputThread extends Thread
{
private Socket socket;
private OutputThread outputThread;
private ObjectOutputStream oos;
private TransEntity transEn;
private boolean isStart = true;
public OutputThread(Socket socket)
{
this.socket = socket;
try
{
oos = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 设置了消息之后,唤醒写线程
public void setMessage(TransEntity transEn)
{
this.transEn = transEn;
synchronized (this)
{
notify();
}
}
public void releaseResouce()
{
try
{
if (oos != null)
oos.close();
if (socket != null)
socket.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run()
{
while(isStart)
{
synchronized (this)
{
try
{
if (transEn != null)
{
wait();
try
{
oos.writeObject(transEn);
oos.flush();
//oos.reset();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
releaseResouce();
}
}
//客户端线程,在客户端线程启动输出和输入线程
public class ClientThread extends Thread
{
private ClientOutputThread out;
private ClientInputThread in;
private Socket socket;
public ClientThread(Socket socket)
{
this.socket = socket;
}
public void run()
{
out = new ClientOutputThread(socket);
in = new ClientInputThread(socket);
out.start();
in.start();
}
public ClientInputThread getIn()
{
return in;
}
public ClientOutputThread getOut()
{
return out;
}
}
//客户端
public class Client
{
private static Client client;
// private boolean isLogin = false;
private Socket socket;
private ClientThread clientThread;
private Client()
{
}
public static synchronized Client getInstance()
{
if (client == null)
{
client = new Client();
}
return client;
}
public boolean startClient()
{
try
{
socket = new Socket();
socket.connect(new InetSocketAddress("10.0.2.2",5345),2000);
if (socket.isConnected())
{
System.out.println("Connecting...");
clientThread = new ClientThread(socket);
clientThread.start();
}
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
return true;
}
// 直接通过client得到读线程
public ClientInputThread getClientInputThread()
{
return clientThread.getIn();
}
// 直接通过client得到写线程
public ClientOutputThread getClientOutputThread()
{
return clientThread.getOut();
}
}
//在onResume里头启动service,由service启动客户端
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
if (!GetMsgService.isStart)
{
Intent service = new Intent(FirstActivity.this, GetMsgService.class);
startService(service);
//isServiceRunning = true;
}
}
报错信息