Model Loading Error

I am trying to load a model using a function, but get an error when the game loads the model (not when the game starts).  The source looks like this:


static public Node LoadModel(String file) {
        Node n;
       
        try {
            URL model = GraphicsSettings.class.getClassLoader().getResource(file);
            ByteArrayOutputStream BO = new ByteArrayOutputStream();
           
            ObjToJme converter = new ObjToJme();
            converter.setProperty("mtllib", model);
           
            converter.convert(model.openStream(), BO);
            n = (Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
        } catch (IOException e) {
            n = new Node("Empty Node Because of Model Loading Error");
            System.out.println("Exception loading " + file + ".");
            e.printStackTrace();
        }



I'm trying to load jmetest/data/models/maggie.obj but when I do I get the following output:

init:
deps-jar:
compile:
run:
Oct 8, 2007 9:20:37 PM com.jme.app.BaseGame start
INFO: Application started.
Oct 8, 2007 9:20:37 PM com.jme.system.PropertiesIO <init>
INFO: PropertiesIO created
Oct 8, 2007 9:20:37 PM com.jme.system.PropertiesIO load
INFO: Read properties
Oct 8, 2007 9:20:38 PM com.jme.input.joystick.DummyJoystickInput <init>
INFO: Joystick support is disabled
Oct 8, 2007 9:20:38 PM com.jme.system.lwjgl.LWJGLDisplaySystem <init>
INFO: LWJGL Display System created.
Oct 8, 2007 9:20:39 PM com.jme.system.lwjgl.LWJGLDisplaySystem getValidDisplayMode
INFO: Selected DisplayMode: 1024 x 768 x 24 @50Hz
Oct 8, 2007 9:20:39 PM com.jme.renderer.lwjgl.LWJGLRenderer <init>
INFO: LWJGLRenderer created. W:  1024H: 768
Oct 8, 2007 9:20:40 PM com.jme.renderer.AbstractCamera <init>
INFO: Camera created.
Oct 8, 2007 9:20:40 PM com.jme.scene.Node <init>
INFO: Node created.
Oct 8, 2007 9:20:40 PM com.jme.util.lwjgl.LWJGLTimer <init>
INFO: Timer resolution: 1000 ticks per second
Oct 8, 2007 9:20:40 PM com.jme.scene.Node <init>
INFO: Node created.
Oct 8, 2007 9:20:40 PM com.jme.scene.Node attachChild
INFO: Child (Menu Text) attached to this node (Text Node)
Oct 8, 2007 9:20:41 PM class monkeysrc.Game start()
SEVERE: Exception in game loop
java.lang.NullPointerException
        at monkeysrc.Engine.GraphicsSettings.LoadModel(GraphicsSettings.java:49)
        at monkeysrc.State2.initState(State2.java:48)
        at monkeysrc.Engine.StateManager.SetState(StateManager.java:27)
        at monkeysrc.State1.updateState(State1.java:72)
        at monkeysrc.Engine.StateManager.update(StateManager.java:38)
        at monkeysrc.Game.update(Game.java:55)
        at com.jme.app.BaseGame.start(Unknown Source)
        at monkeysrc.Game.main(Game.java:47)
Oct 8, 2007 9:20:41 PM com.jme.app.BaseGame start
INFO: Application ending.
BUILD SUCCESSFUL (total time: 4 seconds)


The error points to the line converter.convert(model.openStream(), BO);

Could anyone please help?!?

Try:


URL model = GraphicsSettings.class.getClassLoader().getResource(file);
System.out.println(model);



If that prints "null", then the string you are providing cannot be resolved to a valid url. So the call to model.openStream() throws the NullPointerException, becaue you cannot call methods on null objects.

That was the problem!  I am such an idiot.  But now I'm getting a different error when I try to attach my model to the rootNode.  This is the error - starting with where it prints the name of the file:


file:/home/noah/jme/build/jmetest/data/model/maggie.obj
Oct 9, 2007 6:41:49 AM com.jmex.model.converters.ObjToJme processLine
INFO: Object:MAGGIE
Oct 9, 2007 6:41:49 AM com.jme.scene.Node <init>
INFO: Node created.
Oct 9, 2007 6:41:49 AM com.jme.util.geom.GeometryTool minimizeVerts
INFO: batch: MAGGIE: Batch 0 old: 226 new: 226
Oct 9, 2007 6:41:49 AM com.jme.scene.Node attachChild
INFO: Child (MAGGIE) attached to this node (obj file)
Oct 9, 2007 6:41:49 AM com.jme.util.geom.GeometryTool minimizeVerts
INFO: batch: MAGGIE: Batch 0 old: 225 new: 225
Oct 9, 2007 6:41:49 AM com.jme.scene.Node attachChild
INFO: Child (MAGGIE) attached to this node (obj file)
Oct 9, 2007 6:41:49 AM com.jme.util.geom.GeometryTool minimizeVerts
INFO: batch: MAGGIE: Batch 0 old: 110 new: 110
Oct 9, 2007 6:41:49 AM com.jme.scene.Node attachChild
INFO: Child (MAGGIE) attached to this node (obj file)
Oct 9, 2007 6:41:49 AM com.jme.util.geom.GeometryTool minimizeVerts
INFO: batch: MAGGIE: Batch 0 old: 34 new: 34
Oct 9, 2007 6:41:49 AM com.jme.scene.Node attachChild
INFO: Child (MAGGIE) attached to this node (obj file)
Oct 9, 2007 6:41:49 AM com.jme.util.geom.GeometryTool minimizeVerts
INFO: batch: MAGGIE: Batch 0 old: 86 new: 86
Oct 9, 2007 6:41:49 AM com.jme.scene.Node attachChild
INFO: Child (MAGGIE) attached to this node (obj file)
Oct 9, 2007 6:41:49 AM com.jme.scene.Node <init>
INFO: Node created.
Oct 9, 2007 6:41:50 AM class monkeysrc.Game start()
SEVERE: Exception in game loop
java.lang.NullPointerException
        at monkeysrc.State2.initState(State2.java:54)
        at monkeysrc.Engine.StateManager.SetState(StateManager.java:27)
        at monkeysrc.State1.updateState(State1.java:72)
        at monkeysrc.Engine.StateManager.update(StateManager.java:38)
        at monkeysrc.Game.update(Game.java:55)
        at com.jme.app.BaseGame.start(Unknown Source)
        at monkeysrc.Game.main(Game.java:47)
Oct 9, 2007 6:41:50 AM com.jme.app.BaseGame start
INFO: Application ending.
BUILD SUCCESSFUL (total time: 14 seconds)

Most NullPointerExceptions in StandardGame are related to trying operations that require to be executed in the GL thread from another one. If you are using StandardGame be sure to send the attach code to the GL thread by means of the Game Queue:


GameTaskQueueManager.getManager().update(new Callable<Object>() {
         public Object call() throws Exception {
            rootNode.attachChild( myObject );
            return null;
         }
        });

I'm using basegame.  I don't really have the time to learn standardgame.

Looks like something else is missing. if you could post your code "monkeysrc.State2.initState" around the line 54, i could tell you more…

When you say you don't have time to learn StandardGame I assume you mean you are under a tight schedule for this particular application, but will devote the time later on, right?, otherwise Darkfrog will be really disappointed!  :stuck_out_tongue:

Definitely!  StandardGame sounds really useful.



By the way, the code that's loading the model looks like this:


Node maggie = GraphicsSettings.LoadModel("jmetest/data/model/maggie.obj");
        maggie.setLocalScale(0.1f);
        maggie.setLocalTranslation(0, 0, 5);
        maggie.setModelBound(new BoundingSphere());
        maggie.updateModelBound();
       
        rootNode.attachChild(maggie);      <-- this is the trouble line

Looking at the attachChild() code i really dont see what can throw the NullPointerException, unless your rootNode is null.

Notice that the NullPointer happens in the update, so perhaps there is something in the model that is not being loaded right… like a texture or something. The line attaching the model is NOT the NPE itself.

duenez said:

Notice that the NullPointer happens in the update, so perhaps there is something in the model that is not being loaded right... like a texture or something. The line attaching the model is NOT the NPE itself.

no, as lex said only the rootNode can be null