I wonder if anyone can help me to understand where I could be going wrong with this code; Basically I’m working on a tutorial and calling the class below from another class – and it is getting the following error;
Exception in thread “Thread-1” java.lang.NullPointerException
at org.newdawn.spaceinvaders.TCPChat.run(TCPChat.java:322)
at java.lang.Thread.run(Unknown Source)
I realize the error is being flagged in another class- but I have tested the other class with a small class which sets up a separate thread – and it works fine, but as soon as I try and implement a new thread in this class – it causes all sorts of problems. Am I setting up the thread correctly in this class?
Basically I can set up a thread in this class, with a test loop and it’s fine, but when I bring in the functionality of the rest of the game it sometimes hangs, or does not display at all.
Any suggestions on where I could be going wrong would be greatly appreciated.
Thanks for looking.
NullPointerExceptionis the most common exception and is very easy to identify. It occurs on a number of cases (listed in the linked javadoc), but the most common is calling a method on anullobject. For example if you have:So go to the line of code indicated by the stacktrace (TCPChat.java:322), and check whether there is a
nullobject there on which methods are invoked. If there are, make sure they are notnull, or make anifstatement that checks ifif (obj != null)so that the code is not executed.Update: it seems the
connectButtonisnull. You have to callinitOptionsPane()before you call start the thread.