Odd NullPointer (everyones favorite!)

Ok so just getting to grips with this jME beast and am writing a little simulation program (basically simulates dancing…) anyway, am just at the point of creating a "seperate" class to contain my definition of a "Dancer" which i am thinking should simply return a node so that in the main simulation class i can create an array of them and animate them however. However i am getting an infuriating Null Pointer with the following (Dancer.java):




import com.jme.bounding.BoundingSphere;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;

public class Dancer {
   
   private Node CompleteDancer;
   private int danceNumber;

   public Dancer(int givenDanceNumber) {
      danceNumber = givenDanceNumber;
      CompleteDancer = new Node("Dancer " + danceNumber);
      BuildDancer();
   }
   
   private void BuildDancer() {
      Box body = new Box("body " + danceNumber, new Vector3f(0,0,0), new Vector3f(4,1,4));
      body.setModelBound(new BoundingSphere());
      CompleteDancer.attachChild(body);
      CompleteDancer.updateWorldBound();
   }
   
   public Node getDancer() {
      return CompleteDancer;
   }
   
   public static void main(String args[]) {
      Dancer d = new Dancer(2);
   }
   
}



and i cant really figure out what is causing it. It's dieing on:


Box body = new Box("body " + danceNumber, new Vector3f(0,0,0), new Vector3f(4,1,4));



which is ment to create a cube one unit (not sure what the units represent realy..) up and 4 units across (wide, deep..)

Any help would be totaly appreciated.

Regards
Roja

Can you post the stacktrace?

Not a problem :slight_smile: Here ya go:



31-Mar-2005 18:26:27 com.jme.scene.Node <init>
INFO: Node created.
java.lang.NullPointerException
   at com.jme.scene.Geometry.<init>(Unknown Source)
   at com.jme.scene.TriMesh.<init>(Unknown Source)
   at com.jme.scene.shape.Box.<init>(Unknown Source)
   at Dancer.BuildDancer(Dancer.java:18)
   at Dancer.main(Dancer.java:32)
Exception in thread "main"



Hope that's of some help.. doesnt tell much :(

Just to mention… Maybe help with locating the issue… The code seems to work not a problem when i transfer the code directly into the main Class like so:




import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingSphere;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;

public class DanceSim extends SimpleGame{
   
   private Node[] Dancers;
   private final Vector3f[] StartPosition = {
      new Vector3f(0,10,0),
      new Vector3f(0,20,0),
      new Vector3f(0,30,0),
      new Vector3f(0,40,0),
      new Vector3f(50,10,0),
      new Vector3f(50,20,0),
      new Vector3f(50,30,0),
      new Vector3f(50,40,0)
   };
   private int numDancers;
   
   public DanceSim(int givenNumDancers) {
      numDancers = givenNumDancers;
      Dancers = new Node[numDancers];      //setup dancers array to correct size
      for (int i=0; i < numDancers; i++)
         Dancers[i] = new Node("Dancer" + i);//new Dancer(i).getDancer();   //create dance nodes
      this.setDialogBehaviour(SimpleGame.NEVER_SHOW_PROPS_DIALOG);
   }

   public static void main(String[] args) {
      DanceSim ds = new DanceSim(3);       //sets up dance with 3 dancers
      ds.start();
   }

   private void BuildDancer(int danceNumber) {
      Box body = new Box("body " + danceNumber, new Vector3f(0,0,0), new Vector3f(1,1,1));
      body.setModelBound(new BoundingSphere());
      body.updateModelBound();
      Dancers[danceNumber].attachChild(body);
      Dancers[danceNumber].updateWorldBound();
   }
   
   protected void simpleInitGame() {
      for (int i=0; i < numDancers; i++)
         BuildDancer(i);
      for (int i=0; i < numDancers; i++)
         Dancers[i].setLocalTranslation(StartPosition[i]);   //put dancers in there places
      for (int i=0; i < numDancers; i++)
         rootNode.attachChild(Dancers[i]);
   }
}




could it be that there is an issue in that the second class isnt implementing or extending anything (maybe i missed somthing in the tutorial about it..)

Cheers

Roja

I’m at work so don’t have a good way of testing things, but if you are building your Dancer and in turn your Box before you create the display, do it after.

Posted at the same time… Looks like you are building the dancers after the display is built… so ignore the last post.



I’ll have to look at it when I get a work break.

Much appreciated… it seems to have been sorted by moving from using a constructor in the main class to making more use of the simpleInitGame method.



Help was much appreciated!



Roja