问题描述
报错类/**服务器保持与某个客服端的连接线程类*/packagecom.chat.server.model;importjava.io.*;importjava.net.*;importcom.chat.common.*;publicclassSerConnClientThreadextendsThread{Sockets;booleanisConn=false;publicstaticvoidmain(String[]args){}publicSerConnClientThread(Sockets){this.s=s;isConn=true;}publicvoidrun(){while(isConn){try{ObjectInputStreamois=newObjectInputStream(s.getInputStream());Messagem=(Message)ois.readObject();System.out.println(m.getSender()+"对"+m.getGetter()+"说:"+m.getCon()+"时间:"+m.getSendTime());//转发客服端信息SerConnClientThreadsc=ManageClientThread.getClientThread(m.getGetter());//System.out.println("hereisOK");//ObjectInputStreamtest=newObjectInputStream(sc.s.getInputStream());//System.out.println(newManageClientThread().getClientThread(m.getGetter()));ObjectOutputStreamoos=newObjectOutputStream(sc.s.getOutputStream());报错地方oos.writeObject(m);}catch(SocketExceptione){System.out.println("一个客户端以断开...");isConn=false;}catch(Exceptione){e.printStackTrace();}}}}ManageClientThread类/**线程管理器*/packagecom.chat.server.model;importjava.util.*;publicclassManageClientThread{publicstaticHashMap<String,SerConnClientThread>hm=newHashMap<String,SerConnClientThread>();//在HashMap中为每个客户端线程编号publicstaticvoidaddClientThread(StringthreadId,SerConnClientThreadct){hm.put(threadId,ct);//System.out.print(hm.isEmpty());System.out.println("为线程:"+hm.get(threadId).getName()+"编号:"+threadId);}publicstaticSerConnClientThreadgetClientThread(StringthreadId){return(SerConnClientThread)hm.get(threadId);}}服务端信息服务器已启动,监听端口:9999账号:1密码:123456为线程:Thread-3编号:1账号:2密码:123456为线程:Thread-4编号:22对1说:1时间:07:11:46java.lang.NullPointerExceptionatcom.chat.server.model.SerConnClientThread.run(SerConnClientThread.java:40)
解决方案
解决方案二:
SerConnClientThread.java:40有对象没初始化呗
解决方案三:
SerConnClientThreadsc=ManageClientThread.getClientThread(m.getGetter());已经初始化sc了啊
解决方案四:
sc.s.getOutputStream()得到的值是否为null,测试下看看