NPE on cross-class loadModel

Okay, so I am at a loss. I’ve gone through everything I can think of. I can only assume that I am making a very stupid mistake. I’m kinda new to JME, sorry!



I have three classes, main, InitHex, and SimplexIteration. Init is called by main, simplex is called by init. When I run Init on its own with its own main method, it works fine, as soon as I call it from main, it gives me in NPE. The purpose of the code is to make a hex grid using simplex. Here is my code and the associated errors. I’ll try and keep it minimalistic. Just tell me if you need more, I didn’t want to waste space though.



[java]

public class InitHex{



private AssetManager assetManager;

private Node rootNode;

public void InitHex(AssetManager assetManager, Node rootNode) {

this.assetManager=assetManager;

this.rootNode=rootNode;

}



public void makeHex(float xin, float yin) {



Spatial hex = assetManager.loadModel(“Models/HexPiece_05/HexPiece_05.j3o”);

rootNode.attachChild(hex);

yA = noise*16;

}[/java]



This is the error I get,



Nov 23, 2012 7:35:13 PM com.jme3.app.Application handleError

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

java.lang.NullPointerException

at mygame.InitHex.makeHex(InitHex.java:88)

at mygame.InitHex.makeGrid(InitHex.java:74)

at mygame.Main.simpleInitApp(Main.java:25)

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)



Specifically this line seems to trigger an error,

“Spatial hex = assetManager.loadModel(“Models/HexPiece_05/HexPiece_05.j3o”);”



Thanks in advance for any help. If anyone has questions about using simplex noise or something like that, I’d be happy to help :stuck_out_tongue:

In main() the assetManager etc. haven’t been initialized yet, the application loop first starts in simpleInitApp. Check out the “Hello Loop” and “Hello Application” tutorials.

Well at least you realised you need to pass your asset manager in, that’s further than 99% of the people asking this question got :smiley:



Why does inithex have void, isn’t it a constructor? Most likely it hasn’t been called though or its been called with asset manager null.

Thanks! I’ll try that solution now.



Heh, the only thing I can lay claim to is trying to do my research. I hadn’t done that until about 40 minutes ago. I set InitHex to void because currently it doesn’t return anything. That might change though. You think its a problem?

I just don’t understand how it compiles. Class InitHex, so constructor should be InitHex, constructors don’t have a return type…



Well I guess technically that’s legal (if very odd) Java, but it’s not a constructor.

InitHex here is a contructor (or should be) since it has the same name as the class.

This means that it should follow the syntax of a constructor, which doesn’t specify a returned type.



That way, you can initialize an object, by calling the constructor (with it’s parameters).

Then, you will receive a returned object (instance of the class) you can use to call it’s methods. Since your constructor contains all the objects needed to properly work, it will be ready to serve it’s purpose(s).



The constructor is really the most important method of a class.

1 Like

Actually, yeah, its not exactly a constructor. I’m using simplex noise to generate a hex grid. InitHex creates the hex grid. I guess the name is a little misleading…



Ugh, I’m feeling dumb. I’m trying to initialize assetManager in main() and I don’t know what I’m doing wrong. I’m a little embarrassed to show the code cause I’ve tossed (probably) some unnecessary stuff. But here is my main method.



[java]public class Main extends SimpleApplication {



// not sure if these types are right

protected Application app;

protected AssetManager assetManager;



public static void main(String[] args) {



Main app = new Main();

app.start();



}



@Override

public void simpleInitApp() {



// am I putting this in the wrong place or something?

assetManager = app.getAssetManager();



InitHex ih = new InitHex();

ih.makeGrid();

}[/java]



That just doesn’t work, similar error. If I try to put it in the main method it tells me I can’t cause main is static. If I don’t do either I get the same error as before. Thanks for being patient.

Uh, you overwrite the built-in assetManager variable, hence its null. Remove line 4 and 5, then change the app.getAssetManager simply to getAssetManager, as you are the app object in that method :slight_smile: The main method only has to specify cause its a static method and hence is on the class level and not the object level. Some brushing up of general java skills probably wouldn’t hurt :wink:

Ahhhhh, thanks loopies and zarch, for clarifying. Thanks for the help normen (it worked).



I haven’t programmed in a while, and not in Java since early in high school. I suppose I’ve gotten a little sloppy. Its something I’m just picking up again, so yeah, brushing up on java in progress.