App.getAssetManager returns null when called from an other class

In advance I apologize if my post is not in the well category. I’m a JMonkey newbie.

Well, everything is in the title. So I get stuck there, I try to call this getter from an other class to be able to manage graphics and assets from other part than in the main class.

Thank you very much in advance guys!

1 Like

Without more information… my guess is:

  1. you are very new to Java
  2. you see code examples that extend SimpleApplication that directly refer to a magic ‘assetManager’ and you don’t know where it comes from

If so…

SimpleApplication defines some fields that are available to subclasses. This is an ugly crutch that makes it easy to write tests but makes it very confusing to learn what’s happening.

If you have some other class that is not your main application but it needs things like AssetManager then you will have to pass a reference to those things to that class. For new Java developers, this invariably means trying a bunch of bad things. (For example, it’s quite common for new devs to then just have all of their classes extend SimpleApplication… which is wrong AND doesn’t solve the problem. “How many applications do you want in your application?” etc.)

I highly recommend finding some Java tutorials that teach the basics about object oriented programming. Do enough tutorials until you can understand where the magic ‘assetManager’ reference came from. Hopefully then you will understand how to get it to your own classes… and probably you will also then understand the nice things about app states and why they work the way they do.


Well, thank you for you answer, some time ago I studied 2 years of programmation, especially oriented object, probably I need to refresh some basics, but what I remember is that in a relatively large application, it’s always better to separate the UX and the back-end code, so this is what I try to do when I want to access to texture managers from my visual dedicated package.

So what pattern/which way would you recommand for this? Is there a class more appropriate?

1 Like

pass the assetManager instance as a parameter in your called-class constructor or instance ,

& each class extending SimpleApplication would render a new Game (new viewport , cam , assetManager ,stateManager ,etc) this is an abstract class

1 Like

@vsilaire you can use java the complete Reference 11th edition & start by revising OOP , Inheritance , Polymorphism, Encapsulation,abstraction , interfaces , Streams I/O , Overloading & Overriding , MultiThreading & you are ready to rock it to start using JmonkeyEngine Wiki & Doc

Happy Learning Time !:tada::blush: :partying_face:

1 Like

I’d like to add: The AssetManager is created later during initialization and it’s still null in the “App extends SimpleApplication” constructor.

Thus, if you want to access it or pass its reference around, you have to wait. It’s made available during simpleInitApp().
If you instantiate your other class in this method, App.getAssetManager() shouldn’t return null.

Often it’s easier to make the other class extend BaseAppState and register it with the StateManager.
This way the other class is automatically notified when these things become available in its initialize(Application app) method.