-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.

4

2 回答 2

2

velocitynull,这是显而易见的情况。或者您的World w参数可能是null并导致超类构造函数中的 NPE。null据我所知,这是您的Bullet构造函数中唯一可能存在的两件事。

于 2013-06-16T18:10:52.140 回答
0

如果你不能使用调试器,只需在构造函数的每一行之后放置打印语句,或者你认为异常可以。

于 2013-06-16T18:22:01.073 回答