Animation NullPointerExeption

hi i’ve created this class

   public class UnitGeom {
AssetManager assetManager;
 public AnimChannel channel;
   public AnimControl control;

      Spatial spat;


public UnitGeom(AssetManager assetManager) {
    setAssetManager(assetManager);
    spat = (Spatial) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    spat.setLocalScale(0.5f);
    spat.setLocalTranslation(2, 2, 2);
control = spat.getControl(AnimControl.class);
channel = control.createChannel();
channel.setAnim("stand");

UnitGeom unit= new UnitGeom(spat, channel,control) ;

MyCityBuilder.MainGameClass.units.attachChild(spat);
ArrayList<UnitGeom>arrayList= new ArrayList<UnitGeom>();
arrayList.add(unit);
MyCityBuilder.MainGameClass.setUnitGeom(arrayList);
}

public UnitGeom(Spatial spat,AnimChannel channel,AnimControl control) {
}


public void setAssetManager(AssetManager assetManager) {
    this.assetManager = assetManager;
      }


       public void  channelSetAnim(){//AnimChannel channel
    
   channel = control.createChannel();
   channel.setAnim("Walk");
    //channel.setLoopMode(LoopMode.Loop);
      }
   }

each time i try to

     unitM = new UnitGeom(assetManager);
      getUnitGeom().get(0).channelSetAnim();

it crash with null pointer exeption for

     public void  channelSetAnim(){//AnimChannel channel
   channel.setAnim("Walk");
   
      }

it seems each time channel turns null,ive tried mostly every thing ,but still cant get it work

The code you’ve posted is a mess… it’s hard to tell where one snippet ends and another begins…

For example:

…cannot possibly be in the constructor for UnitGeom but that’s what it looks like.

Whatever class is being returned by this:

has a null channel.

Presuming you’ve read the stack trace correctly. We can’t see the stack trace or the code so we’ll have to trust you.

java.lang.NullPointerException
at MyCityBuilder.UnitGeom.channelSetAnim(UnitGeom.java:68)
at MyCityBuilder.MainGameClass.simpleInitApp(MainGameClass.java:846)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:744)

   public static ArrayList<UnitGeom>unitGeom;
   UnitGeom unitM;

public static void setUnitGeom(ArrayList<UnitGeom> unitGeom) {
    MainGameClass.unitGeom = unitGeom;
}

public static ArrayList<UnitGeom> getUnitGeom() {
    return unitGeom;
}

is in an other big and completly working class,

A few things I can say with certainty:

  1. something on this line:
    MyCityBuilder.UnitGeom.channelSetAnim(UnitGeom.java:68)
    …is null.

  2. you haven’t provided enough code for us to help you.

You have two paths ahead of you… either add some debugging printlns or step through your code in a debugger, etc. and figure out what the issue is.

Or simplify things to a simple complete test that you can post.

men thats all the code… really the rest is not even touching it… i dont know what alse is missing here

Here all i’ve got
2nd class

 package MyCityBuilder;



 import com.jme3.animation.AnimChannel;
   import com.jme3.animation.AnimControl;
  import com.jme3.animation.LoopMode;
      import com.jme3.asset.AssetManager;
          import com.jme3.bounding.BoundingVolume;
          import com.jme3.collision.Collidable;
         import com.jme3.collision.CollisionResults;
       import com.jme3.collision.UnsupportedCollisionException;
      import com.jme3.scene.Geometry;
      import com.jme3.scene.Mesh;
       import com.jme3.scene.SceneGraphVisitor;
       import com.jme3.scene.Spatial;
        import java.lang.reflect.Array;
    import java.util.ArrayList;
       import java.util.Queue;



  public class UnitGeom {
AssetManager assetManager;
    public AnimChannel channel;
     public void setChannel(AnimChannel channel) {
    this.channel = channel;
}
    public AnimControl control;

    Spatial spat;


public UnitGeom(AssetManager assetManager) {
    setAssetManager(assetManager);
    spat = (Spatial) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    spat.setLocalScale(0.5f);
    spat.setLocalTranslation(2, 2, 2);
control = spat.getControl(AnimControl.class);
channel = control.createChannel();
channel.setAnim("stand");

UnitGeom unit= new UnitGeom(spat, channel,control) ;

//MyCityBuilder.MainGameClass.units.attachChild(spat);
jme3test.helloworld.HelloJME3.node.attachChild(spat);
ArrayList<UnitGeom>arrayList= new ArrayList<UnitGeom>();
arrayList.add(unit);
jme3test.helloworld.HelloJME3.setUnitGeom(arrayList);
}

public UnitGeom(Spatial spat,AnimChannel channel,AnimControl control) {
}


public void setAssetManager(AssetManager assetManager) {
    this.assetManager = assetManager;
}


public void  channelSetAnim(){//AnimChannel channel
channel.setAnim("Walk");
    //channel.setLoopMode(LoopMode.Loop);
}

}

here main class
package jme3test.helloworld;

  import static MyCityBuilder.MainGameClass.staticManager;
  import MyCityBuilder.UnitGeom;
      import com.jme3.app.SimpleApplication;
        import com.jme3.asset.AssetManager;
       import com.jme3.material.Material;
        import com.jme3.math.Vector3f;
       import com.jme3.scene.Geometry;
      import com.jme3.scene.shape.Box;
     import com.jme3.math.ColorRGBA;
           import com.jme3.scene.Node;
         import java.util.ArrayList;

   public class HelloJME3 extends SimpleApplication {

 public static AssetManager staticManager;

 public static void main(String[] args){
    HelloJME3 app = new HelloJME3();
    app.start(); // start the game
}
  public static ArrayList<UnitGeom>unitGeom;
UnitGeom unitM;

public static void setUnitGeom(ArrayList<UnitGeom> unitGeom) {
    HelloJME3.unitGeom = unitGeom;
}

  public static ArrayList<UnitGeom> getUnitGeom() {
    return unitGeom;
}
    public static Node node = new Node("NODE");
   public void simpleInitApp() {
 staticManager=assetManager;

rootNode.attachChild(node);
unitM = new UnitGeom(assetManager);
      getUnitGeom().get(0).channelSetAnim();

    }
        }

java.lang.NullPointerException
at MyCityBuilder.UnitGeom.channelSetAnim(UnitGeom.java:69)
at jme3test.helloworld.HelloJME3.simpleInitApp(HelloJME3.java:49)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:744)

channel.setAnim(“Walk”);
is the line

channel just keeps turn null

You are creating a UnitGeom inside of UnitGeom’s constructor. That’s completely bizarre.

And that constructor isn’t doing anything with the channel that is passed so it will be null in that instance.

Learning to code is really hard. Learning to code Java is really hard. Learning to develop games is also really hard.

Learning to do all at the same time is nearly impossible.

I recommend taking a step back and learning some basic Java programming and working your way up from more basic examples.