NullPointerException Help

Hi been working on a my first simple game in JMonkey. Was going well until I ran into this error. Tried changing a few things but with no luck on fixing it. Could someone point out what Im doing wrong?

The code:

[java]
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.asset.AssetManager;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.scene.CameraNode;
import com.jme3.scene.control.CameraControl.ControlDirection;
import com.jme3.math.Quaternion;
import com.jme3.math.FastMath;

/**

  • test

  • @author normenhansen
    */
    public class Main extends SimpleApplication
    {
    Player Player;
    FloorList LevelFloor;
    ObstacleList Obstacles1;
    ObstacleList Obstacles2;
    ObstacleList Obstacles3;
    ObstacleList Obstacles4;

    public static void main(String[] args)
    {
    Main app = new Main();
    app.start();
    }

    @Override
    public void simpleInitApp()
    {
    //Setup Player
    Player = new Player(assetManager);
    Player.Init();

     //Setup Floor
     LevelFloor = new FloorList();
     for(int i = 0; i < 4; i++)
     {
         LevelFloor.Add(assetManager);
     }
     
     //Setup Obstacles
     Obstacles1 = new ObstacleList();
     for(int i = 0; i < 4; i++)
     {
         Obstacles1.Add(assetManager);
     }
     
     Obstacles2 = new ObstacleList();
     for(int i = 0; i < 4; i++)
     {
         Obstacles2.Add(assetManager);
     }
     
     Obstacles3 = new ObstacleList();
     for(int i = 0; i < 4; i++)
     {
         Obstacles3.Add(assetManager);
     }
     
     Obstacles4 = new ObstacleList();
     for(int i = 0; i < 4; i++)
     {
         Obstacles4.Add(assetManager);
     }
    
     
     
     //Setup Camera
     flyCam.setEnabled(false);
     CameraNode CamNode = new CameraNode("Camera Node", cam);
     CamNode.setControlDir(ControlDirection.SpatialToCamera);
     Player.Node.attachChild(CamNode);
     CamNode.setLocalTranslation(new Vector3f(25, 4, -20));
     CamNode.lookAt(Player.Geometry.getLocalTranslation(), Vector3f.UNIT_Y);
     
    
     //Attachments to root node
     rootNode.attachChild(Player.Node);
     
    
     for(int i = 0; i < 4; i++)
     {
        LevelFloor.FloorArray[i].Init(i);
        rootNode.attachChild(LevelFloor.FloorArray[i].Node); 
     }
    
     for(int i = 0; i < 50; i++)
     {
         System.out.println(Obstacles1.ObstacleArray[i]);
         Obstacles1.ObstacleArray[i].Init(); 
         rootNode.attachChild(Obstacles1.ObstacleArray[i].Node); 
     }
             
     for(int i = 0; i < 50; i++)
      {
         Obstacles2.ObstacleArray[i].Init(); 
         rootNode.attachChild(Obstacles2.ObstacleArray[i].Node); 
      }               
             
     for(int i =0; i < 50; i++)
      {
          Obstacles3.ObstacleArray[i].Init();       
          rootNode.attachChild(Obstacles3.ObstacleArray[i].Node); 
      }            
    

    }

    @Override
    public void simpleUpdate(float tpf)
    {
    // LevelFloor.MoveFloor();
    for(int i = 0; i < 4; i++)
    {
    rootNode.attachChild(LevelFloor.FloorArray[i].Node);
    }
    }

    @Override
    public void simpleRender(RenderManager rm)
    {
    //TODO: add render code
    }
    }

[/java]

ObstacleList class.

[java]
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package mygame;

import com.jme3.asset.AssetManager;

/**
*

  • @author Fraser
    */
    public class ObstacleList
    {
    public Obstacle[] ObstacleArray;
    public int Count;

    public ObstacleList()
    {
    this.ObstacleArray = new Obstacle[50];
    }

    public void Add(AssetManager Manager)
    {
    this.ObstacleArray[this.Count] = new Obstacle(Manager);
    this.Count++;
    }
    }

[/java]

Obstacle Class

[java]
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package mygame;
    import com.jme3.scene.shape.Box;
    import com.jme3.scene.Geometry;
    import com.jme3.material.Material;
    import com.jme3.math.ColorRGBA;
    import com.jme3.renderer.RenderManager;
    import com.jme3.asset.AssetManager;
    import com.jme3.math.Vector3f;
    import com.jme3.scene.Node;

/**
*

  • @author Fraser
    */
    public class Obstacle
    {
    Box Box;
    Geometry Geometry;
    Material Material;
    Node Node;

    public AssetManager AssetManager;

    public Obstacle(AssetManager Manager)
    {
    this.AssetManager = Manager;
    }

    public void Init()
    {
    this.Box = new Box(1,1,1);
    this.Geometry = new Geometry(“box”, this.Box);
    this.Material = new Material(this.AssetManager,“Common/MatDefs/Misc/Unshaded.j3md”);
    this.Material.setColor(“Color”, ColorRGBA.Green);
    this.Geometry.setMaterial(this.Material);
    this.Node = new Node(“Obstacle Node”);
    this.Geometry.setLocalTranslation(new Vector3f(0,0,0));
    this.Node.attachChild(this.Geometry);
    }
    }

[/java]

Error:
[java]
Aug 08, 2014 7:01:29 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at mygame.Main.simpleInitApp(Main.java:99)
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)[/java]

Sorry for the large code included. Dont know whats wrong so dont know what Id need to show someone.

It seems to me like I did the same thing with the LevelFloor.FloorArray on line 90 with no problems. I tried to check the order of all my relevant statements and they look ok to me. Any help appreciated, been banging my head for the last 2 days with this.

Presuming this is the real line 99 mentioned in the stack trace, NPEs are pretty easy to decipher:

rootNode.attachChild(Obstacles2.ObstacleArray[i].Node);

One of these things is null:

Irrefutable fact.

It’s easy to see even from your code that the first one isn’t… the second one isn’t. So it must be one of the others.

As a debugging tip, in the amount of time it took you to make this post you could have added a few printlns or stepped through the debugger and you’d know exactly which was null and could trace back from there.

Edit: as a side note… your code would be 1000x easier to read if you’d follow standard capitalization conventions so readers didn’t have to jump all over your code to tell the difference between a static class field reference and a regular field reference. ie: ObstacleArray versus obstacleArray and so on.

1 Like

Also, is there a reason you are reimplementing ArrayList for yourself?

1 Like

Thanks psspeed, managed to find the cause of the error thanks to your advice. I’d just forgotten to change the number of iterations of a loop creating the instances of obstacles. Feel silly now but thats what I get for staying up all night doing coursework.

As for your point on captialisation conventions, I didnt have a clue that was why it was done that way. Thanks again, Im going to read up on that tonight. Been having seperate issues with static and non static methods. Thats why i’m attaching the objects to the root node instead of the floor objects that will have the same movement. Was giving me errors when calling methods in the update class.

I was just implementing it because we did it that way in one of my other modules, also I didnt realise it was already implemented :facepalm: .

Thanks again.