Reputation: 1070
I've searched myself bloody for the answer to this, but none of the solutions that are explained neither on S/O nor anywhere else on the web seem to work.
I've got two parts to my project. One part is the screen itself where SWT renders (and some basic eventhandling is done). The other part is a Network-class which handles server/client communication through sockets.
What I'm currently doing is trying to allow a change of name in the program, so that when the client types "name Josh", it will set the name on the screen to Josh, updating a label that is created. This is where I'm stuck. I created a method called "updater" which will extend to more as time goes on, but right now only updates a label. I created this function inside of the GUI-class, and made it public to be called from the network-class. However, when it is called, I get an error saying that it failed to create that runnable, pointing to a Null pointer exception. I've tried Display.getCurrent();
, Display.getDefault();
and UIPlatform.getWorkBench().getDisplay()
, but none of them seem to work.
Updater method (in GUI class)
public static void updater(String arg)
{
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
if(!lblPlayer2.getText().equals(arg)) { lblPlayer2.setText(arg); }
}
});
}
Call to updater method (from Network-class)
IntroScreen.updater(split[1]);
ERROR
org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.NullPointerException)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Unknown Source)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at client.IntroScreen.open(IntroScreen.java:44)
at client.IntroScreen.main(IntroScreen.java:29) Caused by: java.lang.NullPointerException
at client.IntroScreen$2.run(IntroScreen.java:155)
at org.eclipse.swt.widgets.RunnableLock.run(Unknown Source)
... 5 more
Full source for GUI: http://hastebin.com/abolederul.java
Full source for Network: http://hastebin.com/ipulirifat.java
I've hit a brick wall here. Does anyone have a clue to why this is occuring, and if you do, I'd love it if you could explain how I should be thinking to prevent errors like this going forward.
Upvotes: 3
Views: 1758
Reputation: 200296
You are trying to fix the problem at the wrong end. Your Display
is not null and your Runnable
is successfully accepted by the SWT event queue. What fails is the code within that runnable, so something on this line is null:
if(!lblPlayer2.getText().equals(arg)) { lblPlayer2.setText(arg); }
It could be the lblPlayer2
variable.
Upvotes: 2