请教:输出流的问题
我是编写一个Server和Client用Socket通讯的程序,send_data是要发送的数据,是一个字节数组,并且已经赋值。(因为是模拟无线通讯的,所以要考虑流量问题,必须是用字节数组传输,不能用String 或者对象传输)
Send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Send) {
byte[] bmsg = send_data;
if (ct != null)
ct.sendMessage(bmsg);
}
}
});
OutputStream out;
public void sendMessage(byte[] msg){
try{
out.write(msg);//出错了,请问是什么问题??
out.flush();
cout.println(msg.toString());
cout.flush();
cf.chatArea.append( "你发送 " + msg.toString() + "\n ");
}catch(
IOException e){
e.printStackTrace();
}
}
抛出异常:Exception in thread "AWT-EventQueue-0 "
java.lang.NullPointerException------解决方案--------------------似乎OutputStream out;没有初始化?
out = new DataOutputStream(sock.getOutputStream());