首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请问:输出流的有关问题

2012-01-05 
请教:输出流的问题我是编写一个Server和Client用Socket通讯的程序,send_data是要发送的数据,是一个字节数

请教:输出流的问题
我是编写一个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());

热点排行