I am getting a null pointerexception and I am not sure why.

I tried debuging it by adding a system.out.print statement like so:
System.out.println(GameKeys.mouse[pointer]+"-"+pointer);
The output from this statment was: "null-0"

Error
Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.ia.df.manager.GameInputProcessor.touchDown(GameInputProcessor.java:34)
    at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:300)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:200)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Gamekeys class:
public class GameKeys {
    public static final int MAX_FINGERS = 2; 
    public static Vector3[] mouse = new Vector3[MAX_FINGERS]; // x & y
    public static boolean[] down = new boolean[MAX_FINGERS];
    public static boolean[] pdown = new boolean[MAX_FINGERS];


   public static boolean isDownT(int i) {
        return down[i];
   }

   public static boolean isPressedT(int i) {
        return down[i] && !pdown[i];
   }

   public static boolean isReleasedT(int i) {
        return !down[i] && pdown[i];
   }

   public static void update() {
       for (int i = 0; i < MAX_FINGERS; i++) {
           pdown[i] = down[i];
       }
   }
}
GameInputProcessor class:
public class GameInputProcessor extends InputAdapter {

    public boolean touchDown(int x, int y, int pointer, int button) {
        if(pointer > GameKeys.MAX_FINGERS - 1)
            return true;

        System.out.println(GameKeys.mouse[pointer]+"-"+pointer);
        // out is GameKeys.mouse[pointer] is null

        GameKeys.mouse[pointer].set(x, y, 0);
        GameKeys.down[pointer] = true;
        return true;
   }
}
game class:

// when user touch left side of screen than do some thing

for (int i = 0; i < GameKeys.MAX_FINGERS; i++) {
        if (GameKeys.isPressedT(i)) {
            if (GameKeys.mouse[i].x < Gdx.graphics.getWidth() / 2) {
               // do some thing
            }
        }
  }

Dani AI

Generated

As pointed out, the NullPointerException is happening because the array GameKeys.mouse was allocated but its elements were never constructed — each slot is still null. The debug print from already shows that: the slot at index 0 is null, so calling .set(...) throws the NPE.

The simplest, reliable fix is to create a Vector3 for each slot when GameKeys is initialized. Put this inside GameKeys so the objects exist before any input events arrive:

static {
  for (int i = 0; i < MAX_FINGERS; i++) {
    mouse[i] = new com.badlogic.gdx.math.Vector3();
  }
}

If you prefer lazy initialization, guard touchDown so it creates the Vector3 only when needed:

if (GameKeys.mouse[pointer] == null) GameKeys.mouse[pointer] = new Vector3();
GameKeys.mouse[pointer].set(x, y, 0);

Extra tips and cautions:

  • Use a robust bounds check: if (pointer < 0 || pointer >= GameKeys.MAX_FINGERS) return true; — this avoids out-of-range indices.
  • Don’t reuse one Vector3 for all indices; each finger needs its own object.
  • If input events can fire before your game-start code runs, initialize these objects in the GameKeys class (static block) or ensure you set the input processor only after initialization.
  • If Z isn’t needed, consider Vector2 or simple x/y primitives for slightly simpler code.
  • Verify GameKeys.update() is called once per frame in the right place so pdown/down logic behaves as expected.

These changes will remove the NPE and make the input handling more robust.

So GameKeys.mouse[0] is null, meaning you ctreated the array but you didn't initialise its elements. Maybe you just forgot to do that, or maybe one of your events is being called early in the startup, before you have finished initialising that array?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.