Have I put everything in the right place?

Have I put everything in the right place? Thank you

error :smile:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
	at mygame.GameRunningState.initialize(GameRunningState.java:79)
	at com.jme3.app.state.AppStateManager.initializePending(AppStateManager.java:251)
	at com.jme3.app.state.AppStateManager.update(AppStateManager.java:281)
	at com.jme3.app.SimpleApplication.update(SimpleApplication.java:239)
	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)


**GameRunningState class** 

   package mygame;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.ChaseCamera;
import com.jme3.input.FlyByCamera;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.light.PointLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;

/**
 *
 * @author yemibob
 */




public class GameRunningState extends AbstractAppState implements 
        ActionListener, AnimEventListener {
    
    private ViewPort viewPort;
    private Node rootNode;
    private Node guiNode;
    private AssetManager assetManager;
    private SimpleApplication app;
    public InputManager inputManager;
    private Node localRootNode = new Node("Game  RootNode");
    private Node localGuiNode = new Node("Game  GuiNode");
    public AnimChannel animationChannel;
    public AnimControl animationControl;
    public ChaseCamera chaseCam;
    public Node player;
    public Node Scene;
    public CharacterControl character;
    public BulletAppState bulletAppState;
    public Camera cam;
    public FlyByCamera flybyCamera;
    public BulletAppState bulletAppState;
     public  AppStateManager   stateManager;

   // track directional input
public boolean left = false, right = false, up = false, down = false;
    
    
  
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.app = (SimpleApplication) app;
        this.rootNode = this.app.getRootNode();
        this.viewPort = this.app.getViewPort();
        this.guiNode = this.app.getGuiNode();
        this.assetManager = this.app.getAssetManager();
        this.inputManager = this.app.getInputManager();
         this.bulletAppState = this.stateManager.getState(BulletAppState.class);

        inputManager.setCursorVisible(true);
        this.app.getFlyByCamera().setDragToRotate(false);
        
        rootNode.attachChild(localRootNode);
        guiNode.attachChild(localGuiNode);

        //TODO: initialize your AppState, e.g. attach spatials to rootNode
        //this is called on the OpenGL thread after the AppState has been attached
        
      bulletAppState = new BulletAppState();
     //bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
     stateManager.attach(bulletAppState);
     
     PhysicsTestHelper.createPhysicsTestWorld(rootNode,
         assetManager, bulletAppState.getPhysicsSpace());
  Scene = (Node) assetManager.loadModel ("Models/Hotelroom/Hotelroom.j3o");
  Scene.addControl(new RigidBodyControl(0));
  localRootNode.attachChild(Scene);
  bulletAppState.getPhysicsSpace().addAll(Scene);
  
      

    PointLight lamp = new PointLight();
    lamp.setPosition(Vector3f.ZERO);
    lamp.setColor(ColorRGBA.White);
    localRootNode.addLight(lamp); 
    localRootNode.attachChild(Scene); 
   
        /** A white, directional light source */ 
    DirectionalLight sun = new DirectionalLight();
    sun.setDirection((new Vector3f(1f, 1f, 1f)).normalizeLocal());
    sun.setColor(ColorRGBA.White);
    localRootNode.addLight(sun);     
    localRootNode.attachChild(Scene);
    
    
    player =  (Node) assetManager.loadModel("Models/Main Chr/Cube.mesh.j3o");
    player.setLocalTranslation(1f, 1f, 1f);
    //CapsuleCollisionShape capsule = new CapsuleCollisionShape(3f, 4f); - Original
    CapsuleCollisionShape capsule = new CapsuleCollisionShape(2f, 10f);
    character = new CharacterControl(capsule, 0.05f);
    player.scale(3f);
    player.addControl(character);
    character.setJumpSpeed(20f);

    
     //Maps the Keys to Action/Animation
    inputManager.addMapping("CharLeft",new KeyTrigger(KeyInput.KEY_LEFT));  
    inputManager.addMapping("CharRight",new KeyTrigger(KeyInput.KEY_RIGHT));
    inputManager.addMapping("CharForward",new KeyTrigger(KeyInput.KEY_UP));
    inputManager.addMapping("CharBackward",new KeyTrigger(KeyInput.KEY_DOWN));
    inputManager.addMapping("CharJump",new KeyTrigger(KeyInput.KEY_J));
    inputManager.addListener(this, "CharLeft", "CharRight");
    inputManager.addListener(this, "CharForward", "CharBackward");
    inputManager.addListener(this, "CharJump");

    
 
        
    
        
        
    }
    
    @Override
    public void update(float tpf) {
        //TODO: implement behavior during runtime
        
    flybyCamera.setEnabled(false);
    chaseCam = new ChaseCamera(cam, player, inputManager);
    chaseCam.setSmoothMotion(true);
    chaseCam.setTrailingEnabled(true);
        
        
         
  animationControl = player.getControl(AnimControl.class);
  animationControl.addListener((AnimEventListener) this);
  animationChannel = animationControl.createChannel();
  
    bulletAppState.getPhysicsSpace().add(character);
    localRootNode.attachChild(player);
    
        
        
        
    }
 private Vector3f walkDirection = new Vector3f(0,0,0); //stop
private float airTime = 0;
 public void simpleUpdate(float tpf) { 
   Vector3f camDir = cam.getDirection().clone();
   Vector3f camLeft = cam.getLeft().clone();
   camDir.y = 0;
   camLeft.y = 0;
   camDir.normalizeLocal();
   camLeft.normalizeLocal();
   walkDirection.set(0,0,0);
   
   if (left) walkDirection.addLocal(camLeft);
   if (right) walkDirection.addLocal(camLeft.negate());
   if (up) walkDirection.addLocal(camDir);
   if (down) walkDirection.addLocal(camDir.negate());
   
   if (!character.onGround()) { // Remember to use !character.isOnGround() if the character is a BetterCharacterControl type.
       airTime += tpf;
   } else{ 
       airTime =0;
   } 
   if (walkDirection.lengthSquared() ==0) { 
       if (!"Stand".equals(animationChannel.getAnimationName()))  {
            animationChannel.setAnim("Stand", 1f);
       }
   } else {
       character.setViewDirection(walkDirection);
       if (airTime > .3f) {
           if (!"Stand".equals(animationChannel.getAnimationName())) { 
               animationChannel.setAnim("Stand");
           }
       } else if (!" Walk".equals(animationChannel.getAnimationName())) {
          animationChannel.setAnim(" Walk", 0.1f);
        }
       }
     walkDirection.multLocal(5f).multLocal(tpf); //The use of the first multLocal here is to control the rate of movement multiplier for character walk speed. The second one is to make sure the character walks the same speed no matter what the frame rate is
     character.setWalkDirection(walkDirection); // WALKING!!

     
        
        
        
    }
    
    @Override
    public void cleanup() {
        super.cleanup();
        rootNode.detachChild(localRootNode);
        guiNode.detachChild(localGuiNode);
        
        //TODO: clean up what you initialized in the initialize method,
        //e.g. remove all spatials from rootNode
        //this is called on the OpenGL thread after the AppState has been detached
    }
    
    public void onAnimCycleDone(AnimControl control, 
         AnimChannel channel, String animName) {

    }

public void onAnimChange(AnimControl control, 
        AnimChannel channel, String animName) {

    }


public void onAction(String binding,boolean value, float tpf) {
 if (binding.equals("CharLeft")) {
      if (value) left= true;
      else left = false;
 } else if (binding.equals("CharRight")) {
   if (value) right = true;
   else right = false;
} else if (binding.equals("CharForward")) {
  if (value) up = true;
  else up = false;
} else if (binding.equals("CharBackward")) {
   if (value) down = true;
else down = false; 
   
       
   }
}   
    
}

    **And the main class** 



package mygame;



import com.jme3.app.SimpleApplication;

    



/**
 * test
 * @author 
 */

public class Main extends SimpleApplication {

 public boolean isRunning = false; 
 public GameRunningState gameRunningState;
    
   
    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }
 
  @Override
    public void simpleInitApp() {
   
      gameRunningState = new GameRunningState();
      
      stateManager.attach(gameRunningState);
    
}
    
        public void SimpleUpdate(float tpf) {}

}

Your bug is on this lineā€¦

ā€¦itā€™s hard for us to see which line that is but easy for you.

Thanks for replying here is the line :smile:

this.bulletAppState = this.stateManager.getState(BulletAppState.class);

this.stateManager is null because you never set it.

NPEs are like the easy exception to sort out.

haha Have I set it? I thought

public BulletAppState bulletAppState;
public AppStateManager stateManager;

Do you need another BullletAppState class that you write yourself? That may be the problem
As iā€™m trying to convert it as it was an simple application format before and iā€™m trying to gett used to doing it in appstate format. And only the main class as the switcher between appstates? Iā€™m I doing it wrong here? As I came to understand you should do.

Take a read of this. Itā€™s along the path youā€™re heading. http://wiki.jmonkeyengine.org/doku.php/jme3:beginner:hello_physics

Ok, imagine you have a box on your desk that youā€™ve labeled ā€œstateManagerā€. Look in there. Is there anything in the box? Noā€¦ because you havenā€™t put anything in the box. = is how you put things in the box. NPE is caused by trying to do something with the nothing thatā€™s in the box but expecting it to be something.

It seems you are really new to Javaā€¦ you might be picking the hardest way possible to learn it. I recommend finding a few good Java tutorials to start out with.

Thank you jayfella much appreciated Iā€™ll check that out :slight_smile:

Ok it worked before as a simpleapp format but itā€™s not working now in appstate format? So iā€™m trying to understand wonā€™t work
and iā€™m going to check out java tuts Whatā€™s the extra code i need to put in in appstate format? As iā€™m guessing physics work different in appstate format then simpleapp?

Everything worked in simple app format The chr could walk, scene loaded etc when itā€™s the main class but reading lots in jme3 tuts on the wiki and in forms you should do it in appstates not all in one class.

Also is it correctly the way iā€™m doing things minus the newbiesness making the main class a ā€˜switcherā€™ and then put the rest of the code in appstates etc? Or is there another way or iā€™m missing something?

This code solve it ?

public GameRunningState(AppStateManager stateManager) {
this.stateManager = stateManager;
}

this.stateManager = new AppStateManager();

That creates a new AppStateManager. Iā€™m hesitant to digress any further, but, at the risk of throwing fuel in the fire, steer way, way, way away from the keyword static (newcomers seem to think itā€™s a one-stop solution to all their problems, but itā€™s actually the beginning of all their problems), and do a little reading up on object-orientated programming. With those two directions of advice you should be on your way. We all start somewhere, you may as well do it properly :sunny:

So I should put this whole project on hold and learn java fully or can i do it at the same time? How do I solve this error? Also thank you for the advice

Well, your issue is kind of super-simple entry level Java stuffā€¦ which is why I hesitate because this is the simplest of the 2,000,000,000 other problems you will encounter immediately following.

You are passed the stateManager right in the initialize() method. You can just use it. Set it if you like to your local field but if you donā€™t use it anywhere else then there is no need. This is ā€œfirst weekā€ Java learning about variable scope, really.

Learning programming is hard. Learning 3D game programming is really really hard. Learning to do both at the same time is about the same as learning to ride a bike and shave with a straight razor at the same time. Fraught with peril.

After all, one does not go from ā€œno mathā€ to ā€œcalculusā€ by starting out wanting to solve multivariable differential equations. Thatā€™s a lesson for getting frustrated and giving up.

1 Like