NullPointerException

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

Exception0x0e
Posts: 6
Joined: 16 Jan 2012, 10:42

NullPointerException

Postby Exception0x0e » 02 Mar 2012, 20:05

Hi,

I'm running into a problem and I'm stuck. What I'm doing is the following:
- A user logs on to the server but does not join a room
- The user requests a "quickmatch" thru a serverside extension. If there is a room with user waiting then the user is transfed to that room, otherwise a new room is created.

This works perfectly.

Once in a game room I need the users to send data to the other players in the room. For that I'm using the ObjectMessageRequest object on iPhone and on Android.

Code: Select all

       
SFSObject bericht = new SFSObject();
bericht.putBool("test", true);
sfsClient.send(new ObjectMessageRequest(bericht)); 


On iPhone this (similar) code works. On Android I get a NullPointer Exception (see stacktrace below). What is also weird is that on both iPhone and on Android a call to getJoinedRooms returns an empty array. I know however for sure that the users are in the quickmatch room (I can see that on the server admin page).

What am I doing wrong ?

03-02 20:51:58.066: E/AndroidRuntime(554): FATAL EXCEPTION: main
03-02 20:51:58.066: E/AndroidRuntime(554): java.lang.NullPointerException
03-02 20:51:58.066: E/AndroidRuntime(554): at sfs2x.client.requests.GenericMessageRequest.executeObjectMessage(GenericMessageRequest.java:348)
03-02 20:51:58.066: E/AndroidRuntime(554): at sfs2x.client.requests.GenericMessageRequest.execute(GenericMessageRequest.java:160)
03-02 20:51:58.066: E/AndroidRuntime(554): at sfs2x.client.SmartFox.send(SmartFox.java:887)
03-02 20:51:58.066: E/AndroidRuntime(554): at com.pinkie.sfstest.SmartFoxHelper.sendMatchData(SmartFoxHelper.java:106)
03-02 20:51:58.066: E/AndroidRuntime(554): at com.pinkie.sfstest.SFSTestActivity.sendmatchdata(SFSTestActivity.java:69)
03-02 20:51:58.066: E/AndroidRuntime(554): at com.pinkie.sfstest.SFSTestActivity$3.onClick(SFSTestActivity.java:52)
03-02 20:51:58.066: E/AndroidRuntime(554): at android.view.View.performClick(View.java:2485)
03-02 20:51:58.066: E/AndroidRuntime(554): at android.view.View$PerformClick.run(View.java:9080)
03-02 20:51:58.066: E/AndroidRuntime(554): at android.os.Handler.handleCallback(Handler.java:587)
03-02 20:51:58.066: E/AndroidRuntime(554): at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 20:51:58.066: E/AndroidRuntime(554): at android.os.Looper.loop(Looper.java:123)
03-02 20:51:58.066: E/AndroidRuntime(554): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-02 20:51:58.066: E/AndroidRuntime(554): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 20:51:58.066: E/AndroidRuntime(554): at java.lang.reflect.Method.invoke(Method.java:507)
03-02 20:51:58.066: E/AndroidRuntime(554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-02 20:51:58.066: E/AndroidRuntime(554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-02 20:51:58.066: E/AndroidRuntime(554): at dalvik.system.NativeStart.main(Native Method)
Exception0x0e
Posts: 6
Joined: 16 Jan 2012, 10:42

Re: NullPointerException

Postby Exception0x0e » 02 Mar 2012, 23:32

I found out that only the client that created the room is able to send Objectmessages. When I try to send an ObjectMessage from a client that joined later, the Android version crashes and the iPhone version generates an server side error:

java.lang.IllegalArgumentException: Room with Id: 0, is not found.
com.smartfoxserver.v2.controllers.system.GenericMessage.executeObjectMessage(GenericMessage.java:320)
com.smartfoxserver.v2.controllers.system.GenericMessage.execute(GenericMessage.java:126)
com.smartfoxserver.v2.controllers.SystemController.processRequest(SystemController.java:128)
com.smartfoxserver.bitswarm.controllers.AbstractController.run(AbstractController.java:96)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:680)

So perhaps I'm not transfering the users correctly to the already existing quickmatch room. This is the code I use:

Code: Select all

       
                  // Find a quickmatch game with users waiting
                  MatchExpression exp = new MatchExpression(RoomProperties.IS_GAME, BoolMatch.EQUALS, true).and
                                                         (RoomProperties.HAS_FREE_PLAYER_SLOTS, BoolMatch.EQUALS, true).and
                                                         ("isGameStarted", BoolMatch.EQUALS, false).and
                                                         ("variant",StringMatch.EQUALS,"AV");
       
        List<Room> joinablerooms = this.getApi().findRooms(this.getParentExtension().getParentZone().getRoomList(),exp,0);
       
        trace( String.format("Number  of  rooms found: %d", joinablerooms.size()));
       
        if (joinablerooms.size() > 0)   // NB Users can only be in one room at a time (in this game)
        {
          // Put the user in this group
          
           try {
             
            user.addJoinedRoom(joinablerooms.get(0)); 
            joinablerooms.get(0).addUser(user);
            
         } catch (SFSJoinRoomException e) {
            
            e.printStackTrace();
         }
                     
        } else
        {
         // create a new room and wait for other users.
       }
Exception0x0e
Posts: 6
Joined: 16 Jan 2012, 10:42

Re: NullPointerException

Postby Exception0x0e » 02 Mar 2012, 23:58

Solved it.Pfff. feel like a total newby (which I am b.t.w.).

Should have used this in the extension to add the user to the quickmatch room that was found:

Code: Select all

this.getApi().joinRoom(user, joinablerooms.get(0));

Return to “SFS2X Java / Android API”

Who is online

Users browsing this forum: No registered users and 10 guests