开发者社区> 问答> 正文

自己写的Client端提示空指针问题

public class ChatClient {
static Socket s;
static DataOutputStream dos;
static String str;
static TextField txt;
static TextArea content;
public static void main(String[] args){
    new MyFrame().LaunchFrame();
}

static class MyFrame extends Frame{
    public void LaunchFrame(){
        setLocation(300,300);
        setSize(300,400);
        setBackground(Color.PINK);
        setTitle("WeChat");
        add(txt,BorderLayout.SOUTH);
        add(content,BorderLayout.NORTH);
        pack();
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                disconnected();
                System.exit(0);
            }
        });
        setVisible(true);
    }       
}

public void connect() throws UnknownHostException, IOException{
    s = new Socket("127.0.0.1",8888);
    dos = new DataOutputStream(s.getOutputStream());
    System.out.println("connected");
}

private class TFListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        str = txt.getText().trim();
        content.setText(str);
        txt.setText("");
    }
}

public static void disconnected(){
    try {
        dos.writeUTF(str);
        dos.flush();
        s.close();
        dos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

screenshot

展开
收起
蛮大人123 2016-03-18 16:07:35 2107 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    add(txt,BorderLayout.SOUTH);
    add(content,BorderLayout.NORTH);
    这两句的txtcontent没有new出来。

    2019-07-17 19:06:56
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载