For what I have, please refer to:
I have a problem where when I test the program with "type == 2" which goes through sendToOne() method in Server class, I get a NullPointerException at:
ServerThread.run() line94 -------------> server.sendToOne(distribut
Server.sendToOne() line118 ------------>int r = scBack.write(buffer);
I declared a local ByteBuffer at the top of both methods to clear the buffer used in those methods. Still, NullPointerException is thrown. One possibility I think what's causing the error is the way I deal with the methods in DataHandler class, namely, addSocketChannel() and addUserInfo(). In particular, addSocketChannel() need to be invoked first to add an instance of SocketChannel into a LinkedList and this LinkedList is used when the latter addUserInfo() is called. Since I call addUserInfo() at the Clinet constructor and addSocketChannel() at the Server constructor, the ordering of invokation might not seem to be a problem but the Server calls addSocketChannel() after there is an incoming request for connection meaning that by that time, Clinet is processed. Also if I moved addSocketChannel() to right before the invokation of addUserInfo() in Clinet instead, there is no NullPointer but none of the test System.out outputs are printed. Another possibility is that I have two instances of DataHandler object in Clinet and Server which they are both trying to access the same data. Can this be a problem as well? Since neither Client nor Server can access each other, if this causes a problem do I need to have a connection to a database to store the data which DataHandler is handling? Even though it is indicated where the problem is caused, so far I find no effective method of tracking where in the transfer process or data storage something is going wrong. Please give me a suggestion if someone can guide me in fixing this problem. Thanks.