-4

I am writing a platformer and just programmed this class for a bullet. I am getting an NPE in the constructor and I can't see what is wrong, here is my code:

package com.ncom.src.entity;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import com.ncom.src.Camera;
import com.ncom.src.world.World;

public class Bullet extends HealthEntity {
public Vector2f velocity;
public double angle;
private int timeout = 0;

public Bullet(int lx, int ly, World w, float x, float y) {
    super(lx, ly, 5, 5, w, 0);
    double hyp =  Math.sqrt((x - 600) * (x - 600) + (y - 400) * (y - 400));
    angle = Math.asin(y - 400/ hyp);

    velocity.y = (float) Math.sin(angle) * 0.4f;
    velocity.x = (float) Math.cos(angle) * 0.4f;
}

@Override
public void render(Graphics g, Camera c) throws SlickException {
    g.drawImage(new Image("res/actors/bullet.png"), locationX + c.camModX, locationY + c.camModY);
}

@Override
public void update(GameContainer gc, int delta) throws SlickException {
    if (timeout > 400) {
        this.dead = true;
    }
    else {
        locationX += velocity.x;
        locationY += velocity.y;
        timeout += 0.4f;
    }
}
}

I am basically trying to calculate the x component and the y component of a vector to move the bullet by. The x and y in the constructor stand for where the mouse was clicked.

Dr_N
  • 69
  • 1
  • 2
  • 6

2 Answers2

2

velocity is null, that is the obvious case. Or possibly your World w parameter could be null and cause an NPE in the superclass constructor. That's the only two things that can possibly be null in your Bullet constructor, as far as I can see.

Harald K
  • 26,314
  • 7
  • 65
  • 111
0

If u can't use debugger, just put print statements after every line in your constructor or ever u think exception can be.

Gudron
  • 23
  • 4