NPE when passing data from ThridPersonPlayerNode to Spider Monkey

Hi,
Im new to JME and I have a question for you guys, so this is what I have so far, I want to pass the player coordinates when an action is done in ThirdPersonPlayerNode that I got from the tutorial to the client from SpiderMonkey but i keep getting an NPE when i try to set or get anything from the TPPN class. Let me know if you guys want to see more code.

Thanks for the help. =D

ThirdPersonPlayerNode
[java] public void onAction(String binding, boolean value, float tpf) {
sendPlayerMovePacket();
}

private void sendPlayerMovePacket() {
    float x = getCharacterControl().getPhysicsLocation().getX();
    float y = getCharacterControl().getPhysicsLocation().getY();
    float z = getCharacterControl().getPhysicsLocation().getZ();
    float dirX = getCharacterControl().getWalkDirection().getX();
    float dirY = getCharacterControl().getWalkDirection().getY();
    float dirZ = getCharacterControl().getWalkDirection().getZ();

    clientMain.sendPlayerMovePacket("User", x, y, z, dirX, dirY, dirZ);
}[/java]  

ClientMain

[java]public class ClientMain implements ClientStateListener, ErrorListener<Client> {

public Client myClient;
private static String ip;

public ClientMain(String ip) {
    this.ip = ip;
}

public static void main(String[] args) {
    ClientMain clientApp = new ClientMain("localhost");
    clientApp.clientStart();
}

public void clientStart() {
    System.out.println("Client has been started.");

    try {
        myClient = Network.connectToServer(ip, 6143);
    } catch (IOException ex) {
        Logger.getLogger(ClientMain.class.getName()).log(Level.SEVERE, null, ex);
    }
    myClient.start();

    myClient.addClientStateListener(this);
    myClient.addErrorListener(this);

    registerSerializers();
    registerMessageListeners();
}

private void registerSerializers() {
    Serializer.registerClass(Packet000Message.class);
    Serializer.registerClass(Packet001Move.class);
}

private void registerMessageListeners() {
    myClient.addMessageListener(new ClientListener(), Packet000Message.class);
    myClient.addMessageListener(new ClientListener(), Packet001Move.class);
}

public void clientConnected(Client c) {
    System.out.println("User " + c.getId() + " has connected to the server.");
}

public void clientDisconnected(Client c, DisconnectInfo info) {
    System.out.println("User " + c.getId() + " has disconnected from the server.");
}

public void handleError(Client source, Throwable t) {
    // if (t instanceof exception) { }
    //Add your own code here
}

public void clientDestroy() {
    myClient.close();
}

public void sendPlayerMovePacket(String user, float x, float y, float z, float dirX, float dirY, float dirZ) {
    Message packet = new Packet001Move("User", x, y, z, dirX, dirY, dirZ);
    myClient.send(packet);
}

}[/java]

NPE

[java]run:
Aug 09, 2014 7:17:10 PM com.jme3.system.JmeDesktopSystem initialize
INFO: Running on jMonkeyEngine 3.0.10
Aug 09, 2014 7:17:10 PM com.jme3.system.Natives extractNativeLibs
INFO: Extraction Directory: E:\Google Drive\CODE\JMonkeyEngine\Survival Game
Aug 09, 2014 7:17:11 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Lwjgl 2.9.0 context running on thread LWJGL Renderer Thread
Aug 09, 2014 7:17:11 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Adapter: nvd3dumx,nvwgf2umx,nvwgf2umx
Aug 09, 2014 7:17:11 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Driver Version: 9.18.13.4052
Aug 09, 2014 7:17:11 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Vendor: NVIDIA Corporation
Aug 09, 2014 7:17:11 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: OpenGL Version: 4.4.0
Aug 09, 2014 7:17:11 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Renderer: GeForce GTX 670/PCIe/SSE2
Aug 09, 2014 7:17:11 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: GLSL Ver: 4.40 NVIDIA via Cg compiler
Aug 09, 2014 7:17:11 PM com.jme3.asset.AssetConfig loadText
WARNING: Cannot find loader com.jme3.scene.plugins.blender.BlenderModelLoader
Aug 09, 2014 7:17:11 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Device: OpenAL Soft
Aug 09, 2014 7:17:11 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Vendor: OpenAL Community
Aug 09, 2014 7:17:11 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Renderer: OpenAL Soft
Aug 09, 2014 7:17:11 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Version: 1.1 ALSOFT 1.15.1
Aug 09, 2014 7:17:11 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: AudioRenderer supports 64 channels
Aug 09, 2014 7:17:11 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio effect extension version: 1.0
Aug 09, 2014 7:17:11 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio max auxilary sends: 4
Client has been started.
User 4 has connected to the server.
Aug 09, 2014 7:17:13 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at net.kufel.game.ThirdPersonPlayerNode.sendPlayerMovePacket(ThirdPersonPlayerNode.java:201)
at net.kufel.game.ThirdPersonPlayerNode.onAction(ThirdPersonPlayerNode.java:190)
at com.jme3.input.InputManager.invokeAnalogsAndActions(InputManager.java:276)
at com.jme3.input.InputManager.onMouseMotionEventQueued(InputManager.java:394)
at com.jme3.input.InputManager.processQueue(InputManager.java:829)
at com.jme3.input.InputManager.update(InputManager.java:883)
at com.jme3.app.Application.update(Application.java:604)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:231)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:744)
[/java]

This is where your problem is:
net.kufel.game.ThirdPersonPlayerNode.sendPlayerMovePacket(ThirdPersonPlayerNode.java:201)

I don’t know what the line is but that’s where the problem is.

1 Like
@pspeed said: This is where your problem is: net.kufel.game.ThirdPersonPlayerNode.sendPlayerMovePacket(ThirdPersonPlayerNode.java:201)

I don’t know what the line is but that’s where the problem is.

it is this line of the ThirdPersonPlayerNode class that i posted in the first part of the code above, i did a debug and each of the values is set so im not passing any nulls to the ClientMain class but i still do get the NPE
[java]
clientMain.sendPlayerMovePacket(“User”, x, y, z, dirX, dirY, dirZ);
[/java]
which is passed to this method of the ClientMain class
[java]
public void sendPlayerMovePacket(String user, float x, float y, float z, float dirX, float dirY, float dirZ) {
Message packet = new Packet001Move(user, x, y, z, dirX, dirY, dirZ);
myClient.send(packet);
}
[/java]

this is the code that I get from debugger
[java]
Debugger stopped on uncompilable source code.
Exception java.lang.NullPointerException(<No current context>) breakpoint hit in net.kufel.game.ThirdPersonPlayerNode at line 201 by thread LWJGL Renderer Thread.
Thread LWJGL Renderer Thread stopped at ThirdPersonPlayerNode.java:201.
[/java]

Do you know where I can find an example of passing data from a character into a network packet. Cant find anything specific on the wiki’s maybe then I would be able to figure out why I get the NPE

Then ‘clientMain’ is null.
(or your compiled code and your source code are out of sync so the line number is wrong - 99,999% sure that isn’t the case)

1 Like
@kufel said: it is this line of the ThirdPersonPlayerNode class that i posted in the first part of the code above, i did a debug and each of the values is set so im not passing any nulls to the ClientMain class but i still do get the NPE [java] clientMain.sendPlayerMovePacket("User", x, y, z, dirX, dirY, dirZ); [/java]

clientMain is null. That is the only possible way that the line in question can throw an NPE.

1 Like

Thank You for the help =D