views:

126

answers:

3

Hello, 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.

+2  A: 

NullPointerException is 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 a null object. For example if you have:

String str = null;
str.substring(2,4); // str is null and NullPointerException is thrown

So go to the line of code indicated by the stacktrace (TCPChat.java:322), and check whether there is a null object there on which methods are invoked. If there are, make sure they are not null, or make an if statement that checks if if (obj != null) so that the code is not executed.

Update: it seems the connectButton is null. You have to call initOptionsPane() before you call start the thread.

Bozho
I stuck the chat class up there, thanks for the advice, I will have a look and see if I can see what the problem is at 322.
ivor
@ivor check my update
Bozho
Thanks - I'm having a look now.
ivor
+3  A: 

java.lang.NullPointerException at org.newdawn.spaceinvaders.TCPChat.run(TCPChat.java:322)

Open TCPChat.java source file, peek to line 322 and look around in this particular line where the dot operator . is been used to access some object reference. Something like:

someObject.doSomething();

A NullPointerException on this line means that someObject is null. You cannot access or invoke nothing. To solve such a problem, you just need to ensure that someObject is not null at that moment:

if (someObject == null) {
    someObject = new SomeObject();
}
someObject.doSomething();

Or, you need to skip the whole invocation when it is null:

if (someObject != null) {
    someObject.doSomething();
}

This has by the way nothing to do with threads. It's just a programming error, as with every other RuntimeException.

BalusC
(+1). It's at least the second time today someone doesn't understand NPE .. I just wonder how hard it is to open the javadoc. I can't remember having troubles with NPE when I started programming..
Bozho
Thanks for the advice, This is very helpful.
ivor
@Bozho: you have programmers and programmers. @ivor: you're welcome.
BalusC
+1  A: 

With respect to the title of this question: The thread is behaving correctly. It's your code in the thread that is not ;)

luis.espinal