求助!

woainin 2006-12-22 08:53:01
各位好,
我开始按照一篇文章在自己机上实现了,c/s通信程序,client端是applet,
我后来用visual editer改成了窗口的形式,通过按钮来连接服务器,但连接的时候有异常终止,麻烦大家给我看看是什么原因,我把主要的代码贴出来:
//客户端程序
import java.io.*;
import java.net.*;

public class Client
{
Socket ClientSocket;
DataOutputStream os;
DataInputStream is;
String Line;

public void Client()
{
try
{
ClientSocket=new Socket("115.48.31.46",9000);
DataOutputStream os=new DataOutputStream(ClientSocket.getOutputStream());
DataInputStream is=new DataInputStream(ClientSocket.getInputStream());

} //与服务器建立连接

catch(final IOException e)
{
System.out.println("error");
}

}
}
窗体代码:
public class cli_ve {

private Shell sShell = null; // @jve:decl-index=0:visual-constraint="10,10"
private Button button1 = null;
private Button button2 = null;
private Text text1 = null;
private Client cli=null;
private Button button0 = null;

public static void main(String[] args) {
// TODO 自动生成方法存根
/* Before this is run, be sure to set up the launch configuration (Arguments->VM Arguments)
* for the correct SWT library path in order to run with the SWT dlls.
* The dlls are located in the SWT plugin jar.
* For example, on Windows the Eclipse SWT 3.1 plugin jar is:
* installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar
*/
Display display = Display.getDefault();
cli_ve thisClass = new cli_ve();
thisClass.createSShell();
thisClass.sShell.open();

while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* This method initializes sShell
*/
private void createSShell() {
sShell = new Shell();
sShell.setText("网络通信程序客户端......");
sShell.setSize(new Point(667, 219));
sShell.setLayout(null);

button0 = new Button(sShell, SWT.NONE);
button0.setBounds(new Rectangle(124, 153, 81, 22));
button0.setText("连接服务器");
button0.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
System.out.println("mouseDown()");
cli.Client();//调用client的构造函数连接服务器
}
});
}

}
当我点:"连接服务器"按钮时,报console台输出如下:
mouseDown()
Exception in thread "main" java.lang.NullPointerException
at cs.cli_ve$3.mouseDown(cli_ve.java:74)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:133)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:928)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3348)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2968)
at cs.cli_ve.main(cli_ve.java:35)
请大家帮帮忙!!
...全文
132 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
woainin 2006-12-22
  • 打赏
  • 举报
回复
就class cli_ve里使用个class Client对象而已,为什么会异常?
fool_leave 2006-12-22
  • 打赏
  • 举报
回复
这样的代码想找出来真的难呀

at cs.cli_ve$3.mouseDown(cli_ve.java:74)
at cs.cli_ve.main(cli_ve.java:35)
两个位置标一下呀。


估计是cli.Client();//调用client的构造函数连接服务器
这里的cli是null,没看到new的地方在哪
zdywqy 2006-12-22
  • 打赏
  • 举报
回复
sf

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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