Instance a class

Hello

Excuse, I am newbie in JME and I do not write good Inglish…



My problem is it:



################################### Class environment #########################################

package house;

import com.jme3.app.SimpleApplication;

import com.jme3.light.DirectionalLight;

import com.jme3.math.Vector3f;

import com.jme3.scene.Spatial;



public class environment extends SimpleApplication{



public static void main(String[] args) {

environment app = new environment();

app.start();

}



Spatial environment;



@Override

public void simpleInitApp() {

startEnvironment();

}



public void startEnvironment() {

environment = assetManager.loadModel(“Models/environment/environment.j3o”);

environment.setLocalTranslation(0.0f, -2f, 0.0f);

rootNode.attachChild(environment);



//Aplicate light for show house

DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));

rootNode.addLight(sun);



house house1 = new house();

rootNode.attachChild(house1.startHouse());

}

}



################################### Class house #########################################

package house;

import com.jme3.app.SimpleApplication;

import com.jme3.scene.Spatial;



public class house extends SimpleApplication{



protected Spatial house;



@Override

public void simpleInitApp() {

}



public Spatial startHouse() {

house = assetManager.loadModel(“Models/house/house.j3o”);

house.scale(1f, 1f, 1f);

return house;

}

}

Thanks…

you extend SimpleApplication in both classes, only use one. And pass your simpleApplication reference to your house class, so you can have access to the assetManager, etc

Thanks wezrule



I did what you told me, but the error persist



################################### Class environment #########################################

package house;

import com.jme3.app.SimpleApplication;

import com.jme3.light.DirectionalLight;

import com.jme3.math.Vector3f;

import com.jme3.scene.Spatial;



public class environment extends SimpleApplication{



public static void main(String[] args) {

environment app = new environment();

app.start();

}



Spatial environment;



@Override

public void simpleInitApp() {

startEnvironment();

}



public void startEnvironment() {

environment = assetManager.loadModel(“Models/environment/environment.j3o”);

environment.setLocalTranslation(0.0f, -2f, 0.0f);

rootNode.attachChild(environment);



//Aplicate light for show house

DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));

rootNode.addLight(sun);



house house1 = new house();

rootNode.attachChild(house1.startHouse());

}

}



################################### Class house #########################################

package house;

import com.jme3.scene.Spatial;



public class house {



protected Spatial house;



public Spatial startHouse() {

environment env = new environment();

house = env.getAssetManager().loadModel(“Models/house/house.j3o”);

house.scale(1f, 1f, 1f);

return house;

}

}



Error:



SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at house.house.startHouse(house.java:10)

at house.environment.startEnvironment(environment.java:32)

at house.environment.simpleInitApp(environment.java:18)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:230)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)

at java.lang.Thread.run(Thread.java:722)

Don’t create a new environment inside of house, pass ur environment reference to the house class. This is basic java stuff. Please learn java before continuing, its only gonna get harder.

1 Like

Help me whith one example pease…



I understand:

pass ur environment reference:



house house1 = new house()

house1.startHouse( environment reference ); → it good?



Excuse for any inconvenience…

Ill show you how I do it, try something like this:



In House:





[java]

private AssetManager assetManager;

private Node rootNode;

//Whatever other stuff you need to pass over, maybe flyCam



public house(AssetManager assetManager, Node rootNode){

this.assetManager = assetManager;

this.rootNode = rootNode;

}

[/java]

1 Like

Guys, please learn to use the code blocks so that our eyes stop bleeding:

6 Likes

Oops, forgot to use them sorry. Also when I tried to add this reply I clicked on the reply box in your screenshot :smiley:

1 Like