Hey! I'm working on my assignment (top down shooter/action game) and occasionally I get a null pointer exception on this line when I create a new body:
The bug can be replicated by shooting. Some of the projectiles will throw the error despite the fact that they are all created the same way.
Here's the code for BaseProp from which all entities are based upon. Any ideas? 😢
Code:
body.createFixture( fixtureDef );
The bug can be replicated by shooting. Some of the projectiles will throw the error despite the fact that they are all created the same way.
Here's the code for BaseProp from which all entities are based upon. Any ideas? 😢
Code:
public class BaseProp
{
public BufferedImage image;
public DoubleObject rotation = new DoubleObject(0);
public IntObject instanceID = new IntObject(0);
public IntObject typeID = new IntObject(0);
public CollisionUserData userData;
public Body body;
public BodyDef bodyDef;
public FixtureDef fixtureDef;
public BaseProp( World world, String setImage, int setInstanceID, int setTypeID, float setX, float setY, BodyType setBodyType, float setLinearDamping, float setAngularDamping, float setDensity, boolean isBox )
{
loadImage( setImage );
//Body
bodyDef = new BodyDef();
bodyDef.type = setBodyType;
bodyDef.linearDamping = setLinearDamping;
bodyDef.angularDamping = setAngularDamping;
bodyDef.position = new Vec2( setX, setY);
body = world.createBody( bodyDef );
//Box Shape
if( isBox )
{
PolygonShape shape = new PolygonShape();
shape.setAsBox( ( image.getWidth() / Config.PHYS_RATIO ) * 0.5f, ( image.getHeight() / Config.PHYS_RATIO ) * 0.5f);
fixtureDef = new FixtureDef();
fixtureDef.density = 1f;
fixtureDef.shape = shape;
body.createFixture( fixtureDef );
}
//Circle Shape
else
{
CircleShape shape = new CircleShape();
shape.m_radius = ( image.getHeight() / Config.PHYS_RATIO ) * 0.5f;
fixtureDef = new FixtureDef();
fixtureDef.density = setDensity;
fixtureDef.shape = shape;
//TODO throws null pointer exception.
body.createFixture( fixtureDef );
}
typeID.value = setTypeID;
instanceID.value = setInstanceID;
//Userdata
userData = new CollisionUserData( instanceID, typeID );
body.setUserData( userData );
}
/*
* Protected Methods
*/
protected void loadImage( String imagePath )
{
try
{
image = ImageIO.read( new File( imagePath ) );
}
catch ( IOException e )
{
System.out.println( "Could not load image: " + imagePath );
}
}
/*
* Public Methods
*/
public void setMassData( float mass )
{
//Mass Data
MassData massData = new MassData();
massData.mass = mass;
body.setMassData( massData );
}
public void setBullet( boolean isBullet )
{
body.setBullet( isBullet );
}
public void setAngle( float angle )
{
getBody().setTransform( getBody().getPosition(), angle );
}
public Body getBody()
{
return body;
}
}
{
public BufferedImage image;
public DoubleObject rotation = new DoubleObject(0);
public IntObject instanceID = new IntObject(0);
public IntObject typeID = new IntObject(0);
public CollisionUserData userData;
public Body body;
public BodyDef bodyDef;
public FixtureDef fixtureDef;
public BaseProp( World world, String setImage, int setInstanceID, int setTypeID, float setX, float setY, BodyType setBodyType, float setLinearDamping, float setAngularDamping, float setDensity, boolean isBox )
{
loadImage( setImage );
//Body
bodyDef = new BodyDef();
bodyDef.type = setBodyType;
bodyDef.linearDamping = setLinearDamping;
bodyDef.angularDamping = setAngularDamping;
bodyDef.position = new Vec2( setX, setY);
body = world.createBody( bodyDef );
//Box Shape
if( isBox )
{
PolygonShape shape = new PolygonShape();
shape.setAsBox( ( image.getWidth() / Config.PHYS_RATIO ) * 0.5f, ( image.getHeight() / Config.PHYS_RATIO ) * 0.5f);
fixtureDef = new FixtureDef();
fixtureDef.density = 1f;
fixtureDef.shape = shape;
body.createFixture( fixtureDef );
}
//Circle Shape
else
{
CircleShape shape = new CircleShape();
shape.m_radius = ( image.getHeight() / Config.PHYS_RATIO ) * 0.5f;
fixtureDef = new FixtureDef();
fixtureDef.density = setDensity;
fixtureDef.shape = shape;
//TODO throws null pointer exception.
body.createFixture( fixtureDef );
}
typeID.value = setTypeID;
instanceID.value = setInstanceID;
//Userdata
userData = new CollisionUserData( instanceID, typeID );
body.setUserData( userData );
}
/*
* Protected Methods
*/
protected void loadImage( String imagePath )
{
try
{
image = ImageIO.read( new File( imagePath ) );
}
catch ( IOException e )
{
System.out.println( "Could not load image: " + imagePath );
}
}
/*
* Public Methods
*/
public void setMassData( float mass )
{
//Mass Data
MassData massData = new MassData();
massData.mass = mass;
body.setMassData( massData );
}
public void setBullet( boolean isBullet )
{
body.setBullet( isBullet );
}
public void setAngle( float angle )
{
getBody().setTransform( getBody().getPosition(), angle );
}
public Body getBody()
{
return body;
}
}