Java Socket编程我写了个简单聊天程序 没有用线程 发送在窗口输出流 为什么不能发送消息 难道这样做不行?
内容写不下就贴界面的代码吧我在客户端和服务端都调用Myframe的构造函数,用客户端已声明赋值的cout来给Cout赋值,再用Cout.println();输出,都没有用...
内容写不下 就贴界面的代码吧 我在客户端和服务端 都调用Myframe的构造函数,用客户端已声明赋值的cout来给Cout赋值,再用Cout.println();输出,都没有用线程,直接在窗口界面用输出流输出。可是一直有问题——如下,难道不行么??
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at frame.Myframe.actionPerformed(Myframe.java:80)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
//窗口代码//
import java.awt.event.ActionEvent;
public class Myframe extends JFrame implements ActionListener {
private Button button_1;
private Button button_2;
private TextArea textarea;
private TextField textfield;
PrintWriter Cout;
private String message,name;
public Myframe(String name,PrintWriter cout ) {
super(name+"聊天室");
getContentPane();getContentPane().setLayout(null);
setBackground(Color.LIGHT_GRAY);
setSize(450,323);
setVisible(true);validate();
button_1=new Button("发送");
button_1.setForeground(Color.BLACK);
button_1.setBackground(Color.WHITE);
button_1.setBounds(301, 241, 46, 21);
button_1.addActionListener(this);
getContentPane().add(button_1);
button_2=new Button("下线");
button_2.setForeground(Color.BLACK);
button_2.setBackground(Color.WHITE);
button_2.setBounds(378, 241, 46, 21);
button_2.addActionListener(this);
getContentPane().add(button_2);
textarea=new TextArea();
textarea.setBounds(10, 10, 414, 182);
getContentPane().add(textarea);
textfield=new TextField();
textfield.setBounds(10, 235, 271, 29);
getContentPane().add(textfield);
JLabel label = new JLabel("\u8F93\u5165\u4F60\u7684\u4FE1\u606F\u5185\u5BB9");
label.setBounds(10, 208, 109, 21);
getContentPane().add(label);
setWriter(cout);
this.name=name;
}
public void setWriter(PrintWriter cout){ //设置输出流;
Cout=cout;
}
public void receive(String message){//显示信息;
textarea.append(message+"\r\n");
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==button_1){
Cout.println(name+":\r\n"+textfield.getText()+"\r\n"+"\r\n");
textarea.append("我:\r\n"+textfield.getText()+"\r\n"+"\r\n");
textfield.setText("");//清空输入框
}
if(arg0.getSource()==button_2){
textarea.append("你已下线,发送失败。");
Cout.println("对方已下线。");
}
}
} 展开
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at frame.Myframe.actionPerformed(Myframe.java:80)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
//窗口代码//
import java.awt.event.ActionEvent;
public class Myframe extends JFrame implements ActionListener {
private Button button_1;
private Button button_2;
private TextArea textarea;
private TextField textfield;
PrintWriter Cout;
private String message,name;
public Myframe(String name,PrintWriter cout ) {
super(name+"聊天室");
getContentPane();getContentPane().setLayout(null);
setBackground(Color.LIGHT_GRAY);
setSize(450,323);
setVisible(true);validate();
button_1=new Button("发送");
button_1.setForeground(Color.BLACK);
button_1.setBackground(Color.WHITE);
button_1.setBounds(301, 241, 46, 21);
button_1.addActionListener(this);
getContentPane().add(button_1);
button_2=new Button("下线");
button_2.setForeground(Color.BLACK);
button_2.setBackground(Color.WHITE);
button_2.setBounds(378, 241, 46, 21);
button_2.addActionListener(this);
getContentPane().add(button_2);
textarea=new TextArea();
textarea.setBounds(10, 10, 414, 182);
getContentPane().add(textarea);
textfield=new TextField();
textfield.setBounds(10, 235, 271, 29);
getContentPane().add(textfield);
JLabel label = new JLabel("\u8F93\u5165\u4F60\u7684\u4FE1\u606F\u5185\u5BB9");
label.setBounds(10, 208, 109, 21);
getContentPane().add(label);
setWriter(cout);
this.name=name;
}
public void setWriter(PrintWriter cout){ //设置输出流;
Cout=cout;
}
public void receive(String message){//显示信息;
textarea.append(message+"\r\n");
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==button_1){
Cout.println(name+":\r\n"+textfield.getText()+"\r\n"+"\r\n");
textarea.append("我:\r\n"+textfield.getText()+"\r\n"+"\r\n");
textfield.setText("");//清空输入框
}
if(arg0.getSource()==button_2){
textarea.append("你已下线,发送失败。");
Cout.println("对方已下线。");
}
}
} 展开
2个回答
展开全部
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at frame.Myframe.actionPerformed(Myframe.java:80)
这个异常已经很清晰了, actionPerformed方法里, Myframe.java文件的第80行有空指针异常.
要学会看异常信息哦亲...
at frame.Myframe.actionPerformed(Myframe.java:80)
这个异常已经很清晰了, actionPerformed方法里, Myframe.java文件的第80行有空指针异常.
要学会看异常信息哦亲...
追问
我都赋值了啊 看不到有空值啊 这个信息看了 就后面看不懂 Cout.println(name+":\r\n"+textfield.getText()+"\r\n"+"\r\n");这一行Cout是空值? 亲,请教哦
追答
public Myframe(String name,PrintWriter cout ) {
//.....
}
Myframe初始化时赋值了吗? 看看
建议你用debug模式调试调试,这样很快就能找到问题原因..
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |