I have this structure in my program:
class Node0
private Server server;
...
public void init(){
server.waitConnections();
...
class Server
socket = ss.accept(); // ss :socketServer object
handler = new Handler(socket);
handler.start(); // start handler thread
class Handler
public void run() {
while (true) {
try {
package = (Package) ois.readObject(); // ois :ObjectInputStream
if (package != null) {
this.setPackage(package);
}
} catch (Exception e) {
break;
}
}
But now I need to get the package object in the Handler class in my Node0 class, how could I do that ? I receive the package in the Handler class, but now I should be able to send this object to my Node0 class, but if I try to get this value through:
class Node0
server.getHandler().getPackage();
It launches a NullPointerException.
Any idea how I can do that ?
Why don’t you try to use a callback function? You can’t know when the Handler has set a package. You have to trigger the action from the handler.
Based on your very minimalistic example, not tested or even compiling: