是空对象错误没太仔细看
先检查这个把:
userlist=msnsvr.BuildUserList();下面这个方法里userlist没有实例化?有也被注释掉了.
public String[] BuildUserList()
{
System.out.println("--Step Into BuildUserList--");
ClientConnection o;
System.out.println("--Create ClientConnection o--");
for(int i=0;i<Client_List.size();i++)
{
//userlist=new String[Client_List.size()];
o=(ClientConnection)Client_List.get(i);
userlist[i]=o.clientName;
//System.out.println(userlist[i]);
}
System.out.println("--Build Finished--");
return userlist;
}

解决方案 »

  1.   

    实际上在我Server端的输出是这样的
    *************************************
    -- Server is running on port 8888 --
    *************************************
    ===New client have Connected!===
    Clent Is: Nickname
    Add Into Client_List
    Finish Add
    --Step Into sendBackUserList()--
    --Ready To Go Into BuildUserList()--
    java.lang.NullPointerException
    也就是说并没有进入BuildUserList()这个方法,所以我怀疑是不是违反了什么规则。
    哪位高手能指点我?
      

  2.   

    刚才我又看了一下,由于我的ClientConnection是MsnServer产生的一个线程,我发现在ClientConnection里调用MsnServer里的共有变量,或者带返回值得方法都不成。大概有个眉目了,可是我这里出的问题最多的还是java.lang.NullPointerException
    请教各位,这个错误到底是什么意思?多出线在什么情况下呢
      

  3.   

    //添加socket到列表中发送给客户端
    private void AddSocket(ClientConnection c) {
    System.out.println("Add Into Client_List");
    Client_List.add(c);
    System.out.println("Finish Add");
    clientconnection.sendBackUserList();
             ^^^^^^^^^^^^^^^^^
    }
    这里是不是改成c.sendBackUserList();更好一些呢?
      

  4.   

    有一点需要注意,你的MsnServer里面new了一个ClientConnection ,然后呢,你的ClientConnection 里面又重新new了一个MsnServer;也就是说两个类在循环new对方的实例,到什么时候是个结束呢???
    public ClientConnection()
    {
    try
    {
    svrpnt=new PrintWriter(clientsocket.getOutputStream());
    msnsvr=new MsnServer();//ClientConnection中new了一个MsnServer
    start();
    }
    catch(Exception e){}
    }public MsnServer() throws IOException
    {
    svrSocket=new ServerSocket(8888);
    System.out.println("*************************************");
    System.out.println("-- Server is running on port 8888 --");
    System.out.println("*************************************");
    start();//MsnServer线程开始后就会new一个ClientConnection
    }public void run()
    {
    while (true)
    {
    try
    {
    PortListen();
    buf=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    pnt=new PrintWriter(socket.getOutputStream());
    System.out.println("===New client have Connected!===");

    clientconnection=new ClientConnection();
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^这里new了一个ClientConnection clientconnection.clientsocket=socket;

    //读取客户端用户名给clientconnection
    clientconnection.clientName=CheckUserName(buf.readLine());
    System.out.println("Clent Is: "+clientconnection.clientName);
    //添加socket对象到列表中
    AddSocket(clientconnection);
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    }
      

  5.   

    zyg158(DD)你好
    我也正在考虑这个问题
    MsnServer里是否只负责接收socket连接然后产生一个线程,其它的事情在做一个类去实现比较好呢
      

  6.   

    MsnServer应该只负责新建socket,分配socket给客户通信,至于怎么通信,应该由你分配的socket单独去做,不需要MsnServer参与.当socket通信结束时,通知MsnServer此socket已经完成通信即可
    你的MsnServer类应该时控制和调度类,而不是参与工作的^_^
      

  7.   

    zyg158(DD)你好
    昨天晚上我尝试了很多操作,我把MsnServer里有关用户处理的方法都写道了一个我新建的类中,可是我发现,好像在线程中调用其它类中的方法有点问题,后来我又把那些方法以内部类的形式写到了ClientConnection里面,也不成,最后没辙,都放到ClientConnectiong种作为它自己的方法了。到事能通过,但我的用户列表就不是所有线程公用的了。
    所以我在想,一个单独的线程能不能去访问其它非线程类的方法,理论上我觉得没有问题啊。还有就是这个NullPointerException,到底是代表什么意思?
      

  8.   

    NullPointerException是说对象是空的,还没有初始化或者已被进行系统自动回收你看这样调用可以吗?
    public class file {
    public file() {
    }
    void printf() {
    System.out.println("main thread - " + System.currentTimeMillis() / 1000);
    }
    public static void main(String[] args) {
    file f = new file();
    subthread s = new subthread(f);
    }
    }
    class subthread extends Thread {
    private file my_f = null;
    subthread(file tmp_f) {
    my_f = tmp_f;
    start();
    }
    public void run() {
    while (true) {
    try {
    my_f.printf();
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }