DagagramSocket运行问题

lihaifeng0412 2008-04-03 02:03:04
color=#FF6600]这段客户端程序为什么老是提示错误[/color]

package communicationClient;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class CommunicationClient extends Applet{

boolean isStandalone=false;
JLabel jlabel1=new JLabel();
JLabel jlabel2=new JLabel();
JTextField jtextfield1=new JTextField();
JTextArea jtextarea1=new JTextArea();

private DatagramSocket sendSocket,receiveSocket;
private DatagramPacket sendPacket,receivePacket;

public String getParameter(String key,String def){
return isStandalone ? System.getProperty(key,def) : (getParameter(key)!=null ? getParameter(key) :def);
}
public void init(){

try{
jbInit();
}catch(Exception e){
e.printStackTrace();
}
}

private void jbInit() throws Exception{
jlabel1.setText("通讯记录 : ");
jlabel1.setBounds(new Rectangle(16,9,68,27));
setLayout(null);
jtextarea1.setText("你好!");
jtextarea1.setBounds(new Rectangle(15,48,349,59));

jlabel2.setText("输入通讯内容:");
jlabel2.setBounds(new Rectangle(17,125,92,37));
jtextfield1.setText("通讯内容:");
jtextfield1.setBounds(new Rectangle (127,129,244,33));
jtextfield1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
jtextfield1_actionPerformed(e);
}
});


this.add(jlabel1,null);
this.add(jtextarea1,null);
this.add(jlabel2,null);
this.add(jtextfield1,null);

}
public void start(){
this.waitForPackets();
}
public String getAppletInfo(){
return "Applet Infomation";
}
public String[][] getParameterInfo(){
return null;
}

public void waitForPackets(){
try
{
sendSocket=new DatagramSocket();
receiveSocket=new DatagramSocket(8001);
}
catch(SocketException e){
jtextarea1.append("不能打开数据Socket,或数据Socket 无法与指定端口连接!");
}
while(true){
try{
byte[] buf= new byte[100];
receivePacket=new DatagramPacket(buf,buf.length);
receiveSocket.receive(receivePacket);
jtextarea1.append("\n 服务器 :\t");
byte[] data=receivePacket.getData();
String receivedString=new String(data);
jtextarea1.append(receivedString);
}
catch(IOException e){
jtextarea1.append("网络通讯出现错误,问题在" +e.toString());
}
}

}

public static void main(String[] args){
CommunicationClient applet=new CommunicationClient();
applet.isStandalone=true;
Frame frame;
frame=new Frame(){
protected void processWindowEvent(WindowEvent e){
super.processComponentEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
public synchronized void setTitle(String title){
super.setTitle(title);
this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};

frame.setTitle("Applet Frame");
frame.add(applet,BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width)/2,(d.height - frame.getSize().height)/2);
frame.setVisible(true);
}

void jtextfield1_actionPerformed(ActionEvent e){
try{
jtextarea1.append("\n客户端 : ");
String string=jtextfield1.getText();
jtextarea1.append(string);
byte[] databyte=new byte[100];
string.getBytes(0,string.length(),databyte,0);
sendPacket=new DatagramPacket(databyte,string.length(),InetAddress.getByName("192.168.13.47"),8000);
sendSocket.send(sendPacket);
}
catch(IOException e1){
jtextarea1.append("网络通讯出现错误,问题在 "+e1.toString());
}
}
}


浏览器运行时JAVA控制台总提示:

java.lang.NullPointerException
at communicationClient.CommunicationClient.waitForPackets(CommunicationClient.java:78)
at communicationClient.CommunicationClient.start(CommunicationClient.java:56)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "thread applet-communicationClient\CommunicationClient.class" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


这到底怎么回事啊!
按照一般窗口运行时提示

Exception in thread "main" java.lang.NullPointerException
at communicationClient.CommunicationClient.waitForPackets(CommunicationC
lient.java:78)
at communicationClient.CommunicationClient.start(CommunicationClient.jav
a:56)
at communicationClient.CommunicationClient.main(CommunicationClient.java
:111)
Press any key to continue...



急!!!!!!!!!!!!!!!!!!!!各位高手帮忙啊!不胜感激!
...全文
91 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihaifeng0412 2008-04-03
  • 打赏
  • 举报
回复
等答案,拒绝广告!
mxx123 2008-04-03
  • 打赏
  • 举报
回复
gz
lihaifeng0412 2008-04-03
  • 打赏
  • 举报
回复
就没有人知道嗎
郁闷啊!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧