0

I have been working in LWJGL lately, and I have run into an issue. I made an inflictDamage() method, but it is returning a Null Pointer Exception when I activate it. Here is my code: inflictDamage() method:

public static void inflictDamage(int type, GameObject attacker, GameObject enemy, boolean facingLeft, float damageIncrement, float ehp, float attackerCenter, float enemyCenter){
    float attackX = attackerCenter - 16;
    if(type == 1){
        if(facingLeft == true){
            attackX = attackerCenter - 16;
            if(attackX == enemyCenter){
                ehp -= damageIncrement;
            }
        }
        else if(facingLeft == false){
            attackX = attackerCenter + 16;
            if(attackX == enemyCenter){
                ehp -= damageIncrement;
            }
        }
    }
}

Where it is activated:

package geniushour.game;

import org.lwjgl.input.Keyboard;

public class GOMainCharacter extends GameObject{

public static final int SIZE = 50;
public static final float MaxSpeedX = 4f;
public static final float MaxSpeedY = 8f;

public float velX;
public float velY;

public final float baseOffsetX;
public final float baseOffsetY;

public static float telepointX;
public static float telepointY;

public int cType = 1;

public static boolean faceLeft = true;

public static GOMainCharacter p;
public static GOMonster m;

public GOMainCharacter(float offsetX, float offsetY){

    this.offsetX = offsetX;
    this.offsetY = offsetY;
    this.yPos = SIZE;
    this.xPos = SIZE;
    this.baseOffsetX = this.offsetX;
    this.baseOffsetY = this.offsetY;

    velX = -MaxSpeedX;
    velY = -MaxSpeedY;
}

@Override
void update() {

    if(Keyboard.isKeyDown(Keyboard.KEY_A)){
        telepointX = offsetX;
        telepointY = baseOffsetY;
        faceLeft = true;
        offsetX += velX;
    }
    if(Keyboard.isKeyDown(Keyboard.KEY_D)){
        telepointX = offsetX;
        telepointY = baseOffsetY;
        faceLeft = false;
        offsetX += -velX;
    }
    if(Keyboard.isKeyDown(Keyboard.KEY_S)){
        Draw.inflictDamage(cType, p, m, faceLeft, 10, m.healthPoints, p.getCenterY(), m.getCenterY());
    }

}
    }

I can also provide the GameObject class and such if needed.

  • 1
    I'm going to hazard a guess that you never instantiate `m` and/or `p`. It's not entirely obvious why you pass both the instances and values of their properties to the method. – Andy Turner Nov 03 '15 at 00:48
  • If you could post the stack trace that would be helpful as well. A null pointer is fairly well documented, but the stack would help pinpoint where exactly you went wrong (although I think Andy Turner has it right). – Nick DeFazio Nov 03 '15 at 02:45
  • The stack trace is: Exception in thread "main" java.lang.NullPointerException at geniushour.game.GOMainCharacter.update(GOMainCharacter.java:65) at geniushour.game.Game.update(Game.java:29) at geniushour.game.Main.update(Main.java:44) at geniushour.game.Main.gameLoop(Main.java:68) at geniushour.game.Main.main(Main.java:33) – NightTerror6 Nov 03 '15 at 03:01
  • @AndyTurner I'm kind of a half-newb to java. I know some things and some things I don't. Would you mind explaining how I would instantiate the GameObject(s) m and p? – NightTerror6 Nov 03 '15 at 03:04
  • You need to assign them a non-null value, for example `p = new GOMainCharacter(...)`. – Andy Turner Nov 03 '15 at 07:47

0 Answers0