这是个java聊天程序的客户端这是程序提示我的的错误,我不知道该怎么改,请高手帮我解决下。 
Exception in thread "main" java.lang.NullPointerException
at ChatClient.<init>(ChatClient.java:23)
at ChatClient.main(ChatClient.java:117)import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ChatClient extends JFrame{
private JTextField inputBox;
private JTextArea outFrame;
private ObjectOutputStream outputS;
private ObjectInputStream inputS;
private String message="";
private String chatServer;
private Socket toclient;

//初始化聊天服务
public ChatClient(String srvhost){
super("Client");
chatServer=srvhost;
//设置客户连接的服务器
Container container=getContentPane();
//建立输入框
inputBox.setEnabled(false);
inputBox.addActionListener(
//监听
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMsg(event.getActionCommand());
}
//向服务器端发送消息
}
);
container.add(inputBox,BorderLayout.NORTH);
outFrame=new JTextArea();
container.add(new JScrollPane(outFrame),BorderLayout.CENTER);
setSize(280,160);
setVisible(true);
//输出输出框
}

//连接服务器端
public void connectClient()
{
try{
//创建用于连接的Socket
connect2Server();
//得到输入输出流
getStreams();
//建立连接
processConnection();
//关闭连接
closeConnection();
}catch(EOFException eofException){
System.out.println("Server terminated connection");
}catch(IOException ioException){
ioException.printStackTrace();
}
//捕获异常
}

//获取输入输出流
private void getStreams() throws IOException{
//输出
outputS=new ObjectOutputStream(toclient.getOutputStream());
outputS.flush();
//输入
inputS=new ObjectInputStream(toclient.getInputStream());
outFrame.append("\nGet I/O streams\n");
}

//连接服务器端
private void connect2Server() throws IOException{
outFrame.setText("连接中......\n");
toclient=new Socket(InetAddress.getByName(chatServer),4000);
//连接信息显示
outFrame.append("连接至:"+toclient.getInetAddress().getHostName());
}

private void processConnection() throws IOException{
//输出框
inputBox.setEnabled(true);
do{
//读入信息并输出
try{
message=(String)inputS.readObject();
outFrame.append("\n"+message);
outFrame.setCaretPosition(outFrame.getText().length());
}catch (ClassNotFoundException classNotFoundException){
outFrame.append("\nUnknown object type received");
}
}while(!message.equals("服务器端>>TERMINATE"));
} //关闭输入输出流,关闭连接,注意顺序
private void closeConnection() throws IOException{
outFrame.append("\n关闭连接");
outputS.close();
inputS.close();
toclient.close();
}

//给服务器端发送消息
private void sendMsg(String message){
try{
outputS.writeObject("客户端"+message);
outputS.flush();
outFrame.append("\n客户端"+message);
}catch(IOException ioException){
outFrame.append("\nError writing object");
}
}

//main()方法
public static void main(String[] args){
ChatClient beginning;
if(args.length==0)
beginning=new ChatClient("127.0.0.1");
else
beginning=new ChatClient(args[0]);
beginning.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
beginning.connectClient();
}
}

解决方案 »

  1.   

    beginning=new ChatClient(args[0]);  这里beginning是空值,你的程序就走不到if流程中,一直走的是else流程,你把args[0]打印看看结果
      

  2.   

    我遇到过报同样的异常 当时我是一个参数为null 不知道对你有没有帮助
      

  3.   

    用插入源代码编辑一下,这样很难看。
    // 建立输入框
    inputBox= new JTextField();
    inputBox.setEnabled(false);
    inputBox.addActionListener(
    // 监听
    new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    sendMsg(event.getActionCommand());
    }
    // 向服务器端发送消息
    });
    inputBox 空指针,先new 一个。后面还有不少错误!你自己看看吧。
      

  4.   

    inputBox 空指针。一个一个解决