Can't load model on own object/entity

Hey Guys
I have a problem with a class I created myself. Its called “Entity” and an object from this class should display
an ordinary item like a crate or something in my scene.
The reason I made this is, that I want to add certain parameters like “isDestroyable” etc.
Later, I also want to add physics to this object so you can mess around with it in the game.

Here is my code:
[java]
package mygame.entities;

import com.jme3.asset.AssetManager;
import com.jme3.scene.Spatial;

public class Entity {

private AssetManager assetManager;
private Spatial sceneModel;
private String itsName;
private int life;
private boolean destroyAble;
private boolean destroyed;

public Entity(String name, int lifes, boolean destroyable, float x, float y, float z) {
    
    itsName = name;
    life = lifes;
    destroyAble = destroyable;
    sceneModel = assetManager.loadModel("Models/woodlog.j3o");

}

public String getName() {
    return itsName;
}

public int getLife() {
    return life;
}

public void setLife(int lifes) {
    life = lifes;
}

public boolean isDestroyable() {
    return destroyAble;
}

public boolean isDestroyed() {
    if (destroyAble && life <= 0) {
        destroyed = true;
    } else {
        destroyed = false;
    }
    return destroyed;
}

}
[/java]

So in my Main Class i typed:
[java]
private Entity test = new Entity(“test”, 3, true, -2f, 30f, 1f);
[/java]

This should create this object at the named position (last 3 floats) and it should have a 3D model
(Models/woodlog.j3o)
But I get this error:

Exception in thread “main” java.lang.NullPointerException
at mygame.entity.Entity.<init>(Entity.java:22)
at mygame.Main.<init>(Main.java:52)
at mygame.Main.main(Main.java:58)
Java Result: 1

It points to this line:
sceneModel = assetManager.loadModel(“Models/woodlog.j3o”);

Can anyone tell my please why it does that?
I also tried to do the following
rootNode.attachChild(sceneModel);

But that does not work because my class does not have a rootNode.
I would be very grateful if anyone can help me :slight_smile:

Cheers

  • Daniel

The assetmanger rootnode ect does not magically appear, they are part of application, and if you need them somewhere else, you have to provide them there,
eg
in simpleInit
new Entity(assetManager,rootNode)

I strongly suggest to read a few java basic tutorial before working with 3d, as doing both at the same time is really hard.

Thanks for the answer! But can you explain it more detailed?
Well I have a lot of java experience. This is just the first time I work with 3D and jMonkey. :wink:

I know that I have to provide them but that was my question:
How can I provide the needed things, or what is needed that this class can hold a 3D model?
I dont find a good solution in the documentation…

And if I understand your solution right, it does not fix the null pointer exception.

If you have experience in Java it should be no problem for you assigning the existing AssetManager in the Application instance to your assetManager variable there, right? Or do you mean “JavaScript experience”?

No I mean Java.
Yeah thats why I wonder myself why my code is not working.
First off, I never wanted to assign the assetManager. I created another one as you can see.
My question is why I get an error. The error does not say that I assigned something wrong.
Maybe it is the solution for my problem but then please explain how I should solve this problem.

“simpleInit
new Entity(rootNode, assetManager)”

Does not help me that much…

Why doesn’t it help you? As Empire said, you don’t configure your assetManager etc. so it obviously cannot work. And theres no need to make another instance either, you’d have to configure it with data from your main app anyway. Also, I don’t see where you create an instance of AssetManager at all? In your constructor you use the assetManager variable, which is obviously null.

Oh yeah you are right. Well now I know why I seem to be kinda noobish :smiley:
Of course I am not a pro, but I know all bascis. Well its my first subject at school and I already coded 2D games. Its been a while…
Anyways, Im sorry that was a dumb mistake.

Ok I tried to solve the problem the way Empire provided:

[java]
public class Entity {

private AssetManager assetManager;
public Spatial sceneModel;
private String itsName;
private int life;
private boolean destroyAble;
private boolean destroyed;

public Entity(AssetManager manager, String name, int lifes, boolean destroyable, float x, float y, float z) {
    assetManager = manager;
    itsName = name;
    life = lifes;
    destroyAble = destroyable;
    sceneModel = assetManager.loadModel("Models/woodlog.j3o");
    //rootNode.attachChild(test.sceneModel);

} ...

[/java]

In Main it says now:
[java]
private Entity test = new Entity(assetManager, “test”, 3, true, -5f, 30f, 1f);
[/java]

Still the same error… why?

If its the same error (line 22) then try clean&build, maybe the new class hasn’t been built. Otherwise, if you say “in Main” I hope you don’t mean in the actual main method, as said, do it in simpleInit, the assetManager in Application is still null when the main method is run.

Wow you are my hero! Clean & build fixed it :slight_smile: Thank you guys a lot!
Now I have a beatiful 3D model in my scene :slight_smile: