I'm creating a simple chat application(Desktop Application) for my own study and I'm using netty library for my Client and Server.
I'm starting the client from Thread: new Thread(new Client()).start();
, I do this from my Helper Class
.
When the Client become Connected to the Server, I want to access the MainController
and set the Label
on it to Connected
.
I'm using Guava Eventbus
to accomplish this.
I do the following code to implement it.
From my MainController where I subscribe the function that will change the Text of the Label:
public class MainController implements Initializable{
@FXML Label label_status;
public MainController(){}
@Override
public void initialize(URL location, ResourceBundle resources) {
/**Some Code Here...**/
}
/**Subscribe Eventbus function**/
@Subscribe
public void changeLabelStatus(String status) {
try{
label_status.setText(status);
}catch (Exception e){
System.out.println(TAG + "Failed to Change the status of Label. >> " + e.toString());
}
}
}
From the Handler of Client where I want to post the Status of the Client:
public class ClientHandler extends SimpleChannelInboundHandler<Object>{
EventBus eventBus;
MainController mainController;
public ClientHandler(){
eventBus = new EventBus();
mainController = new MainController();
eventBus.register(mainController);
}
/**Change the Status when the Client become connected to Server**/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(TAG + "Successfully Connected to Server.);
eventBus.post("Connected"); /**Post here**/
}
}
To check if this implementation of EventBus will work, I tried to println
from the Subscribe function and it works,
but when I tried to label_status.setText(status);
to change the Text
of Label
I get java.lang.NullPointerException
error.
I have no idea why, this is my first time of using both library, I read the guide and example for EventBus and from my understanding this how I do it. What's wrong with my code? How can I achive what I want?
Note: I'm using JavaFX
for this application.
UPDATE:
I give up using Guava Eventbus, I used greenrobot/EventBus with it's latest jar now.