因此,此问题适用于熟悉jMonkey 3 Engine的您。我的simpleUpdate()
循环中包含以下代码:
@Override
public void simpleUpdate(float lastTimePerFrame) {
if (load) {
if (frameCount == 1) {
Element element = nifty.getScreen("loadlevel").findElementByName("loadingtext");
textRenderer = element.getRenderer(TextRenderer.class);
inputManager.setCursorVisible(true);
flyCam.setDragToRotate(true);
CubesTestAssets.registerBlocks();
setProgress(0.2f, "Registering Blocks");
} else if (frameCount == 2) {
initBlockTerrain();
setProgress(0.4f, "Generating Chunk");
} else if (frameCount == 3) {
initControls();
initPlayer();
setProgress(0.6f, "Setting Up Player");
} else if (frameCount == 4) {
viewPort.setBackgroundColor(ColorRGBA.Cyan);
setProgress(0.8f, "Creating Sky");
} else if (frameCount == 5) {
inputManager.setCursorVisible(false);
flyCam.setDragToRotate(false);
setProgress(1.0f, "Done");
} else if (frameCount == 6) {
nifty.gotoScreen("end");
nifty.exit();
guiViewPort.removeProcessor(niftyDisplay);
}
frameCount++;
}
cubesSettings = CubesTestAssets.getSettings(this);
playerControl = new CharacterControl();
float playerMoveSpeed = ((cubesSettings.getBlockSize() * 2.5f) * lastTimePerFrame);
Vector3f camDir = cam.getDirection().mult(playerMoveSpeed);
Vector3f camLeft = cam.getLeft().mult(playerMoveSpeed);
walkDirection.set(0, 0, 0);
if(arrowKeys[0]){ walkDirection.addLocal(camDir); }
if(arrowKeys[1]){ walkDirection.addLocal(camLeft.negate()); }
if(arrowKeys[2]){ walkDirection.addLocal(camDir.negate()); }
if(arrowKeys[3]){ walkDirection.addLocal(camLeft); }
walkDirection.set(0, 0, 0);
walkDirection.setY(0);
playerControl.setWalkDirection(walkDirection);
cam.setLocation(playerControl.getPhysicsLocation());
}
我留下了这个nullPointerException:
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.jme3.bullet.objects.PhysicsCharacter.setWalkDirection(PhysicsCharacter.java:115)
at com.bminus.Main.simpleUpdate(Main.java:219)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:242)
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)
第219行是此行:
playerControl.setWalkDirection(walkDirection);
我已经检查过,并且
playerControl
和walkDirection
都不为空。我的问题是:还有什么可以为空?如何解决此错误?为什么会这样呢?我如何防止这种情况将来发生?对于这个问题的任何帮助将不胜感激,如果您需要查看完整的代码,请问!我非常高兴与您分享!提前致谢! 最佳答案
如果要实例化没有参数的CharacterControl
,则不会初始化character
类中的PhysicsCharacter
成员。
完整的跟踪将是:
=> simpleUpdate()
=> playerControl.setWalkDirection()
=> character.setWalkDirection(Converter.convert(walkDirection, tempVec));
=> NullPointerException on character object
当您调用方法
playerControl.setWalkDirection
时,就是这个问题。因此,您必须使用另一个构造器,例如
CharacterControl(CollisionShape shape, float stepHeight)
。CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
playerControl = new CharacterControl(capsuleShape, 0.5f);
有关参数值和其他内容,请参见:
CapsuleCollisionShape
CharacterControl
PhysicsCharacter