Multiple classes

Hi, i followed the tutorials for beginners and after the “Hello animation” tutorial I tried to put it in different classes. But I always get a nullpointerexeption. Below are my classes and error



Main:

[java]public class Main extends SimpleApplication{



public static void main(String args) {

Main app = new Main();

//remove initial screen

app.setShowSettings(false);

AppSettings settings = new AppSettings(true);

settings.put("Width", 800);

settings.put("Height", 600);

settings.put("Title", "");

settings.put("VSync", true);

settings.put("Samples", 4);

app.setSettings(settings);

app.start();

}



@Override

public void simpleInitApp() {

/*

//wereld inladen

Box b = new Box(Vector3f.ZERO, 100, 10, 100);

Geometry geom = new Geometry("Box", b);

geom.updateModelBound();



//plaatsen van texture op wereld

Material mat = new Material(assetManager, "Common/MatDefs/Misc/SimpleTextured.j3md");

Texture tex_gr = assetManager.loadTexture("Textures/grass_1.jpg");

mat.setTexture("m_ColorMap", tex_gr);

geom.setMaterial(mat);

*/



viewPort.setBackgroundColor(ColorRGBA.LightGray);



//licht plaatsen

DirectionalLight dl = new DirectionalLight();

dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());

rootNode.addLight(dl);



//inladen van player

Player pl = new Player(); //zo gaat het wel, alleen het manneke is ni te zien :confused:

//pl.initPlayer(); //da gaat ni, nullpointer… wss doek iet verkeerd aja :confused:



//rootNode.attachChild(geom);

rootNode.setCullHint(CullHint.Never);



}



@Override

public void simpleUpdate(float tpf) {



}



@Override

public void simpleRender(RenderManager rm) {

//TODO: add render code

}

}[/java]



Player:

[java]public class Player extends Main implements AnimEventListener {



private AnimChannel channel;

private AnimControl control;

Node player;



public Player() {

initKeys();

initPlayer();

}



// Nullpointer dus hier… van die pl.initPlayer(); in main

private void initPlayer(){

player = (Node) assetManager.loadModel("Models/CBoy/cubeboyMesh.mesh.xml");

player.setLocalScale(0.5f);

rootNode.attachChild(player);





control = player.getControl(AnimControl.class);

control.addListener(this);

channel = control.createChannel();

channel.setAnim("Stand");

}



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

if (animName.equals("Walkcycle")) {

channel.setAnim("Stand", 0.50f);

channel.setLoopMode(LoopMode.DontLoop);

channel.setSpeed(1f);

}

}



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

throw new UnsupportedOperationException("Not supported yet.");

}



private void initKeys() {

inputManager.addMapping("Walkcycle", new KeyTrigger(KeyInput.KEY_Z));

inputManager.addMapping("Wave", new KeyTrigger(KeyInput.KEY_W));

inputManager.addListener(actionListener, "Walkcycle");

inputManager.addListener(actionListener, "Wave");

}



private ActionListener actionListener = new ActionListener() {

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals("Walkcycle") && !keyPressed) {

if (!channel.getAnimationName().equals("Walkcycle")) {

channel.setAnim("Walkcycle", 0.50f);

channel.setLoopMode(LoopMode.Loop);

}

}

if (name.equals("Wave") && !keyPressed){

if (!channel.getAnimationName().equals("Wave")){

channel.setAnim("Wave", 0.50f);

channel.setLoopMode(LoopMode.Loop);

}

}

}

};

}[/java]



error:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at mygame.Player.initKeys(Player.java:58)
at mygame.Player.(Player.java:28)
at mygame.Main.simpleInitApp(Main.java:55)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:186)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:134)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:183)
at java.lang.Thread.run(Thread.java:662)


the model is one that I made in blender, i tested it in the main class and it works fine..
someone can help me?
thx in advance

You extend the main class and have an inputManager variable because of that but since you instantiate it as your own object and you dont actually start an application the inputManager is null. Dont extend main but pass a variable pointing to the application in the constructor of your class and then use that like aplication.getInputManager().

ik denk dat het het beste is om als het kan, te proberen 1 klasse te gebruiken. als dit niet kan, of het is veel gemakkelijker in meerdere klassen, hang ik alles aan de rootNode in de Main class. daar plaats ik dan ook de code om andere klassen te updaten. Het is ook meestal het beste om alle controls in de main klasse te regelen.

i think it’s the best to ,where possible, to use only one class. and when i have to use an other class, i always attach them too the rootNode and update them in the main class. it’s also easyest to react to input in the main class.

Thx guys! I’ll try what you said. sadly enough there will be added a lot more, so classes are needed :frowning: aren’t there any examples with classes? I already searched here and googled it, but didn’t find any, but i also can’t believe there aren’t examples with classes :s

Using classes like this is without question the way to go. All the examples won’t do this just to keep things as simple as possible and remain focussed on what they’re trying to teach you, the 3d engine itself, without over-complicating things.


  • Andy.