Page 1 of 1

helper.getUserById throws Exception (not string issue)

Posted: 29 Sep 2010, 12:29
by gl0om

Code: Select all

public void _handleRequest(String cmd, org.json.JSONObject data, User fromUser, int fromRoom)
{
   ... skipped ...
   int avatar_id = data.getInt("avatar_id");
   User target = helper.getUserById(avatar_id); //line 195
   ... skipped ...
}


throws

java.lang.NullPointerException
at test.CustomOSExtension._handleRequest(CustomOSExtension.java:195)
at com.smartfoxserver.openspace.OpenSpaceExtension.handleRequest(OpenSpa
ceExtension.java:202)
at it.gotoandplay.smartfoxserver.controllers.ExtensionHandler.processEve
nt(ExtensionHandler.java:500)
at it.gotoandplay.smartfoxserver.controllers.ExtensionHandler.run(Extens
ionHandler.java:344)
at java.lang.Thread.run(Thread.java:619)

I have no idea why. Please help.[/code]

Posted: 29 Sep 2010, 19:24
by BigFIsh
It usually throws that exception when you attempt to call a method on a null variable.

so for example, if data was null - java would throw the java.NullPointerException if you try and do this --> data.getSomething()

Posted: 29 Sep 2010, 19:56
by gl0om
No, data is ok. First thing I checked was data. Tracing avatar_id shows me correct number.

Posted: 29 Sep 2010, 20:31
by gl0om
oops, my inattention, helper was uninitialized...

Posted: 30 Sep 2010, 03:11
by appels
i'm getting the same although all seems to work fine :
2010/09/30 05:39:33.621 - [ WARNING ] [id: 27] (JavascriptExtension.logASError): Error in extension [ customLogin.as ]: TypeError: Cannot call method "getUserId" of null (customLogin.as#1607) Internal: 32 -- Line number: 31 in file: customLogin.as

I call the getUserId() each time a user is logged.
Is there a way to avoid it getting called if the data is a null value ?

Posted: 30 Sep 2010, 04:40
by BigFIsh
well, you could do something like this

if (data != null)
data.getUserId()

but, it's best to avoid that where possible. Try and find out the cause of null value.

Posted: 30 Sep 2010, 09:25
by appels
yes, i was already trying to find out what's causing it. Just can't seem to find it for the moment, will do some more debugging.