程序编译通过,我写的服务器段程序能正常监听(执行正常,由于客户端没连上也不知道功能能行否),但是客户端运行出现界面后单击按钮就异常了
当单击(确定)按钮进行调用out.writeUTF()方法的时候出现空指针异常:
122行 out.writeUTF(str);??????????
d:\Java>java Zhuce
java.lang.NullPointerException
at Zhuce.actionPerformed(Zhuce.java:122)
at java.awt.Button.processActionEvent(Button.java:381)
at java.awt........................等等一系列系统类错误。下边是我的可能你们看很简单的代码,求高手帮我看看;小弟初学,见笑了
import java.sql.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.util.StringTokenizer;public class Zhuce extends Frame implements ActionListener, Runnable
{
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Panel panel3 = new Panel();
TextField text1,text2,text3,text4;
Button queding,quxiao,fanhui;
Label zhanghao,key,mima,tishi;
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
Thread thread;
public Zhuce() {
super("注册添加用户");
setBackground(Color.gray);
setBounds(50, 50, 300, 400);
setVisible(true);
setResizable(false);
validate();
// panel1.setSize(100,200);
// panel2.setSize(100,200);
text1=new TextField(12);
text2=new TextField(12);
text3=new TextField(12);
text4=new TextField(16);
text4.setEditable(false);
text4.setForeground(Color.red);
zhanghao=new Label("请输入要注册的帐号:",Label.CENTER);
key=new Label("请输入帐号密码:",Label.CENTER);
mima=new Label("请确认帐号密码:",Label.CENTER);
tishi=new Label("系统提示:",Label.CENTER);
queding= new Button("确定");
quxiao= new Button("取消");
fanhui= new Button("返回");
text2.setEchoChar('*');
text3.setEchoChar('*');
///////////////////////////////////////////
panel1.setLayout(new GridLayout(4,1));
panel1.add(zhanghao);
panel1.add(key);
panel1.add(mima);
panel1.add(tishi);
panel2.setLayout(new GridLayout(4,1));
panel2.add(text1);
panel2.add(text2);
panel2.add(text3);
panel2.add(text4);
panel3.add(queding);
panel3.add(quxiao);
panel3.add(fanhui); add(panel1, BorderLayout.WEST);
add(panel2, BorderLayout.EAST);
add(panel3, BorderLayout.SOUTH);
queding.addActionListener(this);
quxiao.addActionListener(this);
fanhui.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); }
public void start() {
if (socket != null && in != null && out != null) {
try {
socket.close();
in.close();
out.close();
}
catch (Exception ee) {}
}
try {
socket = new Socket("localhost", 6661);
//socket=new Socket(this.getCodeBase().getHost(), 6661);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ee) {
text4.setText("连接失败");
} if(thread ==null){
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void run() {
String s=null;
while(true)
{
try{
s=in.readUTF();
text4.setText(s);
}
catch (IOException e){
text4.setText("与服务器已断开");
break;
}
} }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==queding) {
if(text2.getText().equals(text3.getText())){
String name, mima;
name=text1.getText();
mima=text2.getText();
String str=name+","+mima;
if(str.length()>0){
try{
out.writeUTF(str);///////////////就这儿啊,这个方法不是系统定义的么?怎
////////////么会空指针呢?我先前定义了阿 }
catch(IOException e1){
text4.setText("与服务器已断开");
}
}
}
else
text4.setText("两次密码输入不一致!");
}
if (e.getSource()==quxiao) {
text1.setText(null);
text2.setText(null);
text3.setText(null);
text4.setText(null);
}
else if(e.getSource()==fanhui){
this.setVisible(false);
}
}public static void main(String args[]) {
Zhuce qq = new Zhuce();
qq.pack();
}
}
当单击(确定)按钮进行调用out.writeUTF()方法的时候出现空指针异常:
122行 out.writeUTF(str);??????????
d:\Java>java Zhuce
java.lang.NullPointerException
at Zhuce.actionPerformed(Zhuce.java:122)
at java.awt.Button.processActionEvent(Button.java:381)
at java.awt........................等等一系列系统类错误。下边是我的可能你们看很简单的代码,求高手帮我看看;小弟初学,见笑了
import java.sql.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.util.StringTokenizer;public class Zhuce extends Frame implements ActionListener, Runnable
{
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Panel panel3 = new Panel();
TextField text1,text2,text3,text4;
Button queding,quxiao,fanhui;
Label zhanghao,key,mima,tishi;
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
Thread thread;
public Zhuce() {
super("注册添加用户");
setBackground(Color.gray);
setBounds(50, 50, 300, 400);
setVisible(true);
setResizable(false);
validate();
// panel1.setSize(100,200);
// panel2.setSize(100,200);
text1=new TextField(12);
text2=new TextField(12);
text3=new TextField(12);
text4=new TextField(16);
text4.setEditable(false);
text4.setForeground(Color.red);
zhanghao=new Label("请输入要注册的帐号:",Label.CENTER);
key=new Label("请输入帐号密码:",Label.CENTER);
mima=new Label("请确认帐号密码:",Label.CENTER);
tishi=new Label("系统提示:",Label.CENTER);
queding= new Button("确定");
quxiao= new Button("取消");
fanhui= new Button("返回");
text2.setEchoChar('*');
text3.setEchoChar('*');
///////////////////////////////////////////
panel1.setLayout(new GridLayout(4,1));
panel1.add(zhanghao);
panel1.add(key);
panel1.add(mima);
panel1.add(tishi);
panel2.setLayout(new GridLayout(4,1));
panel2.add(text1);
panel2.add(text2);
panel2.add(text3);
panel2.add(text4);
panel3.add(queding);
panel3.add(quxiao);
panel3.add(fanhui); add(panel1, BorderLayout.WEST);
add(panel2, BorderLayout.EAST);
add(panel3, BorderLayout.SOUTH);
queding.addActionListener(this);
quxiao.addActionListener(this);
fanhui.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); }
public void start() {
if (socket != null && in != null && out != null) {
try {
socket.close();
in.close();
out.close();
}
catch (Exception ee) {}
}
try {
socket = new Socket("localhost", 6661);
//socket=new Socket(this.getCodeBase().getHost(), 6661);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ee) {
text4.setText("连接失败");
} if(thread ==null){
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void run() {
String s=null;
while(true)
{
try{
s=in.readUTF();
text4.setText(s);
}
catch (IOException e){
text4.setText("与服务器已断开");
break;
}
} }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==queding) {
if(text2.getText().equals(text3.getText())){
String name, mima;
name=text1.getText();
mima=text2.getText();
String str=name+","+mima;
if(str.length()>0){
try{
out.writeUTF(str);///////////////就这儿啊,这个方法不是系统定义的么?怎
////////////么会空指针呢?我先前定义了阿 }
catch(IOException e1){
text4.setText("与服务器已断开");
}
}
}
else
text4.setText("两次密码输入不一致!");
}
if (e.getSource()==quxiao) {
text1.setText(null);
text2.setText(null);
text3.setText(null);
text4.setText(null);
}
else if(e.getSource()==fanhui){
this.setVisible(false);
}
}public static void main(String args[]) {
Zhuce qq = new Zhuce();
qq.pack();
}
}
}
public void start() {
着两行去掉就行了。