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...



急!!!!!!!!!!!!!!!!!!!!各位高手帮忙啊!不胜感激!
...全文
97 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihaifeng0412 2008-04-03
  • 打赏
  • 举报
回复
等答案,拒绝广告!
mxx123 2008-04-03
  • 打赏
  • 举报
回复
gz
lihaifeng0412 2008-04-03
  • 打赏
  • 举报
回复
就没有人知道嗎
郁闷啊!
资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 松下电工数字压力传感器用户手册详细介绍了DP-100系列数字压力传感器,涵盖其技术参数、操作方法及适用场景等,适用于各类需要精准压力测量的工业环境。 双屏显示:主屏与输出动作同步,可同时显示当前值和基准值,便于实时监控与调整。显示屏为12段字母数字显示,数字清晰易读。 三色指示:屏幕颜色随传感器状态变化(红、绿、橙),便于快速判断工作状态。 紧凑结构:尺寸仅□30mm,适合空间狭窄的安装环境。 多种操作模式:提供RUN模式(日常操作)、菜单设定模式(深入设置如输出模式切换)及PRO模式(高级功能如应差调整、复制设定)。 安全认证:DP-101(A)/102(A)型号通过特定认证,确保产品安全可靠。 复制功能:可通过数据通信将主传感器设定内容复制到其他传感器,减少人工设定错误,节省时间。 高性能传感:具备高精度,分辨率1/2,000,反应时间2.5ms(最长5,000ms可调),温度特性±0.5%F.S.,重复精度±0.1%F.S. 电子元件吸附检测:监测吸盘是否成功吸附电子元件。 总压力监测:测量管道或容器内的压力水平。 空气泄漏检测:通过压力变化检测泄漏情况。 DP-101□:适用于低压环境(-100kPa至100kPa)。 DP-102□:适用于高压环境(0kPa至1MPa)。 订购时需根据实际需求选择合适型号,考虑传感器的适用范围和工作条件。手册提供详细订购流程及注意事项,包括相关认证信息(如韩国S标志)。 复制功能:通过数据通信将主传感器设定复制到其他传感器,支持多种设定模式,避免设定错误,节省时间。 操作模式:RUN模式用于日常监控,菜单设定模式用于深入设置,PRO模式提供高级功能。 使用前需仔细阅读手册,了解各功能使用方法。遵循安全指南,正确安装和使用传感器,避免损坏。对于

67,549

社区成员

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

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