java udp聊天 报java.lang.NullPointerException 急~~~~~

来源:百度知道 编辑:UC知道 时间:2024/05/20 02:45:45
public class UDPClient extends Frame {

TextArea ta = null;
Button b1 = null;
String str = null;
//byte[] buff=null;
//ByteArrayOutputStream baos = null;
//DataOutputStream dos = null;
//DatagramSocket ds = null;
//DatagramPacket dp = null;
byte[] buff=null;

public void Laundframe() throws IOException {
setTitle("b");
setLocation(200, 300);
setSize(400, 400);
setVisible(true);
this.setLayout(new GridLayout(2, 1));
ta = new TextArea("", 8, 30, TextArea.SCROLLBARS_NONE);

b1 = new Button("发送给a");
b1.addActionListener(new MonArea(this));
this.add(ta);
this.add(b1);

this.setResizable(false);
pack();
this.addWindowListener(new Windowclosed());
//sconnect();
}

// ////////////////窗口关闭////////////////////
class Windowclosed extends WindowAdapter {
pub

String s= new String(ff.str);
str声明的时候是null,用null来实例化就空指针了
在actionPerformed里面给str赋的值
ff没有用这个方法,所以str还应该是null

ff先调用一下actionPerformed方法
str不就有值了嘛!
ff.actionPerformed();
重写的方法为什么不能调用呢,不调用这个方法str就没有值啊在你的程序里

估计是ff.str没有值,你可以直接打印这个试下
或者把代码改成
String s = "";
if(ff.str == null||ff.str.equals(""))
s = "null value!";
else
s = new String(ff.str);

if(s.equals("") || s==null){
这里可以去做一些,空的处理。比如,弹出一个对话框。
return;
}