Line throws NullPointerException when rendered

Hi,



I’m working on the weapon system for my game and this is the update method of a controller I have:

public void update(float speed) {

   if (vessel.getWeapon().isTriggerDown()) {

      

      // Try to fire a shot, if the weapon isn’t finished reloading it

      // will return null.

      Shot shot = vessel.getWeapon().fire();

      if (shot == null) return;

      

      // Get hit results.

      PickResults results = new BoundingPickResults();

      //results.clear();

      scene.findPick(shot, results);

      System.out.println(results.getNumber());

      

      // Handle hit results.

      if (results.getNumber() > 0) {

         // TODO Handle hit results

         System.out.println(shot.getShooter().getName() + " hit " + results.getPickData(0));

      }

      

      // Create a line.

      Vector3f vertex = {

            shot.getOrigin(),

            shot.getDirection().mult(1000)

      };

      ColorRGBA color = {

            new ColorRGBA(1,0,0,1),

            new ColorRGBA(1,0,0,1)

      };       

      scene.attachChild(new Line("Line", vertex, null, color, null));

   }

}



When isTriggerDown() == true I get this:
INFO: Child (Line) attached to this node (rootNode)

java.lang.NullPointerException

at com.jme.scene.Geometry.applyStates(Unknown Source)

at com.jme.scene.Geometry.draw(Unknown Source)

at com.jme.scene.Line.draw(Unknown Source)

at com.jme.scene.Spatial.onDraw(Unknown Source)

at com.jme.scene.Node.draw(Unknown Source)

at com.jme.scene.Spatial.onDraw(Unknown Source)

at com.jme.renderer.lwjgl.LWJGLRenderer.draw(Unknown Source)


Any ideas?



/Per

Hello Per,



it seems to me that your line has no states updated,



try :



Line line = new Line(“Line”, vertex, null, color, null);

line.updateRenderStates();

scene.attachChild(line);



hope this help but i don’t know if it’s the good way to do it, any other advice ?



Adenthar.

Thanks, that did the trick!



However I’m having further problems, but I’ll post them on a new thread.

you only need to call the updateRenderStates method on the top level of the scenegraph (this will handle all nodes).

you only need to call the updateRenderStates method on the top level of the scenegraph (this will handle all nodes).


Ah i see, but that put me a question on my mind : since it seems that it is in a controller, does calling updateRenderStates only on this node saves time ?

or is it a better method to always put an updateRenderStates in the main update loop ?

bye

Adenthar.

If you are changing only a single node’s states, then calling it on that single node is best. However, every node needs this call at least once, so I was pointing out calling this on the root node during initialization will process all the nodes.



It was probably a very confusing thing to say since you are talking about an update method. :slight_smile:

thanks a lot mojo ! it’s very clear now :smiley:



Adenthar