是空对象错误没太仔细看
先检查这个把:
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;
}
先检查这个把:
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;
}
*************************************
-- 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()这个方法,所以我怀疑是不是违反了什么规则。
哪位高手能指点我?
请教各位,这个错误到底是什么意思?多出线在什么情况下呢
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();更好一些呢?
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);
}
}
}
我也正在考虑这个问题
MsnServer里是否只负责接收socket连接然后产生一个线程,其它的事情在做一个类去实现比较好呢
你的MsnServer类应该时控制和调度类,而不是参与工作的^_^
昨天晚上我尝试了很多操作,我把MsnServer里有关用户处理的方法都写道了一个我新建的类中,可是我发现,好像在线程中调用其它类中的方法有点问题,后来我又把那些方法以内部类的形式写到了ClientConnection里面,也不成,最后没辙,都放到ClientConnectiong种作为它自己的方法了。到事能通过,但我的用户列表就不是所有线程公用的了。
所以我在想,一个单独的线程能不能去访问其它非线程类的方法,理论上我觉得没有问题啊。还有就是这个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();
}
}
}
}