Hello,
I’m I made a 2d platfrom game and now I am porting it to android. Except for the framework, I am re using most of the code from the windwos version and for some reason this gives me an null pointer exception on android.
This is the playState code( handles all the objects rendering,updating,loading level,removing and ading objects and etc).
public class PlayState extends State {
public java.util.List<GameObject> object = new ArrayList<GameObject>();
private int LEVEL = 1;
private GameObject tempObject;
private Rect moveRight, moveLeft, moveRightExit, moveLeftExit, jump;
@Override
public void init() {
object.clear();
loadLevel();
moveLeft = new Rect(10, 470, 170, 530);
moveLeftExit = new Rect(0, 440, 200, 550);
moveRight = new Rect(200, 470, 360, 530);
moveRightExit = new Rect(170, 440, 390, 550);
jump = new Rect(790, 470, 950, 530);
}
@Override
public void update() {
synchronized (object) {
for (int i = 0; i < object.size(); i++) {
tempObject = object.get(i);
if (tempObject != null) {
tempObject.update(object);
}
}
}
}
@Override
public void render(Painter g) {
g.drawImage(Assets.background, 0, 0);
synchronized (object) {
for (int i = 0; i < object.size(); i++) {
tempObject = object.get(i);
tempObject.render(g);
}
}
g.drawRect(10, 470, 170, 530);
g.drawRect(200, 470, 360, 530);
g.drawRect(790, 470, 950, 530);
}
@Override
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
int action = e.getAction() & MotionEvent.ACTION_MASK;
for (int i = 0; i < object.size(); i++) {
tempObject = object.get(i);
if (tempObject.getId() == ObjectId.Player) {
switch (action) {
case MotionEvent.ACTION_UP:
if (moveLeft.contains(scaledX, scaledY)) {
tempObject.setMovingLeft(false);
}
if (moveRight.contains(scaledX, scaledY)) {
tempObject.setMovingRight(false);
}
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_DOWN:
if (jump.contains(scaledX, scaledY)) {
if (tempObject.getVelY() == 0
&& tempObject.isJumping() == false) {
tempObject.setVelY((float) -11.5);
tempObject.setJumping(true);
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_MOVE:
if (moveLeft.contains(scaledX, scaledY)) {
tempObject.setMovingLeft(true);
tempObject.setMovingRight(false);
}
if (moveLeftExit.contains(scaledX, scaledY)
&& !moveLeft.contains(scaledX, scaledY)) {
tempObject.setMovingLeft(false);
}
if (moveRight.contains(scaledX, scaledY)) {
tempObject.setMovingRight(true);
tempObject.setMovingLeft(false);
}
if (moveRightExit.contains(scaledX, scaledY)
&& !moveRight.contains(scaledX, scaledY)) {
tempObject.setMovingRight(false);
}
break;
}
}
}
return true;
}
public void addObject(GameObject object) {
this.object.add(object);
}
public void removeObject(GameObject object) {
this.object.remove(object);
}
public void clearLevel() {
synchronized (object) {
object.clear();
}
}
private void loadLevel() {
Bitmap image = Assets.loadLevel(LEVEL);
int w = image.getWidth();
int h = image.getHeight();
for (int xx = 0; xx < h; xx++) {
for (int yy = 0; yy < w; yy++) {
int pixel = image.getPixel(xx, yy);
int red = Color.red(pixel);
int green = Color.green(pixel);
int blue = Color.blue(pixel);
Log.d("load level", "" + red + "," + green + "," + blue);
if (red == 102 && green == 68 && blue == 0) { // / block
addObject(new Block(xx * 32, yy * 32, 0, ObjectId.Block));
}
if (red == 102 && green == 51 && blue == 0) { // /block
// 1
addObject(new Block(xx * 32, yy * 32, 1, ObjectId.Block));
}
if (red == 102 && green == 34 && blue == 0) { // /block
// 2
addObject(new Block(xx * 32, yy * 32, 2, ObjectId.Block));
}
if (red == 102 && green == 17 && blue == 0) { // /block 3
addObject(new Block(xx * 32, yy * 32, 3, ObjectId.Block));
}
if (red == 255 && green == 255 && blue == 255) { // /block 4
addObject(new Block(xx * 32, yy * 32, 4, ObjectId.Block));
}
if (red == 255 && green == 255 && blue == 245) { // /block
addObject(new Block(xx * 32, yy * 32, 7, ObjectId.Block));
}
if (red == 0 && green == 255 && blue == 34) { // /block up
addObject(new Block(xx * 32, yy * 32, 5, ObjectId.Block));
}
if (red == 0 && green == 245 && (blue > 20 && blue < 40)) { // /block
// up1
addObject(new Block(xx * 32, yy * 32, 6, ObjectId.Block));
}
if (red == 136 && green == 136 && blue == 136) { // /box
addObject(new Box(xx * 32, yy * 32, ObjectId.Box));
}
if (red == 0 && green == 0 && blue == 255) { // / player
addObject(new Player(xx * 32, yy * 32, ObjectId.Player));
}
if (red == 255 && green == 17 && blue == 17) { // ///ex
addObject(new Ex(xx * 32, yy * 32, ObjectId.Ex));
}
}
}
}
public int getLevel() {
return LEVEL;
}
public void setLevel(int LEVEL) {
this.LEVEL = LEVEL;
}
}
this is the code of the object that when he gets hit I use the die() method to remove it:
public class Ex extends GameObject {
private int width = 32, height = 64;
GameObject tempObject;
int delay = 2000;
boolean timer = false;
private Rect bounds = new Rect();
private Rect boundsRight = new Rect();
private Rect boundsLeft = new Rect();
private Rect boundsTop = new Rect();
private Rect boundsMiddle = new Rect();
private float distanceX, distanceY;
PlayState playState;
private RandomNumberGenerator rand = new RandomNumberGenerator();
public Ex(float x, float y, ObjectId id) {
super(x, y, id);
alive = true;
}
@Override
public void update(List<GameObject> object) {
x += velX;
y += velY;
if (x < 0) {
x = 0;
}
if (x > 938) {
x = 938;
}
if (falling) {
velY += gravity;
if (velY > MAX_SPEED) {
velY = MAX_SPEED;
}
}
for (int i = 0; i < object.size(); i++) {
tempObject = object.get(i);
distanceX = x - tempObject.getX();
distanceY = y - tempObject.getY();
if (distanceX < 32 && distanceX > -32 && distanceY < 100
&& distanceY > -100) {
if (tempObject.getId() == ObjectId.Block) { // // Block
// collision
if (getBounds().intersect(tempObject.getBounds())
&& velY >= 0) {
y = tempObject.getY() - height;
falling = false;
velY = 0;
} else {
falling = true;
}
if (getBoundsLeft().intersect(tempObject.getBounds())) {
x += 4;
}
if (getBoundsRight().intersect(tempObject.getBounds())) {
x -= 4;
}
if (getBoundsTop().intersect(tempObject.getBounds())
&& velY < 0) {
velY = 0;
y += 5;
}
}
if (tempObject.getId() == ObjectId.ChangeableBlock) { // //
// ChangeableBlock
// collision
if (getBounds().intersect(tempObject.getBounds())
&& velY >= 0) {
if (tempObject.getChanged() == false) {
y = tempObject.getY() - height;
falling = false;
velY = 0;
jumping = false;
}
} else {
falling = true;
}
}
if (tempObject.getId() == ObjectId.Box) { // // Box collision
if (getBoundsTop().intersect(tempObject.getBounds())) {
die();
}
}
if (tempObject.getId() == ObjectId.Spikes) { // // Spikes
// collision
if (getBoundsMiddle().intersect(tempObject.getBounds())) {
die();
}
}
if (tempObject.getId() == ObjectId.Axe) { // // Axe
// collision
if (getBoundsMiddle().intersect(tempObject.getBounds())) {
die();
}
}
}
}
}
@Override
public void render(Painter g) {
if (alive) {
g.drawImage(Assets.ex, (int) x, (int) y);
}
}
private void die() {
playState.addObject(new Bodypart(x, y, 1, 1, 10,
-10, ObjectId.Bodypart));
}
@Override
public Rect getBounds() {
bounds.set((int) x + 4, (int) y + 35, (int) x + 28, (int) y + 67);
return bounds;
}
@Override
public Rect getBoundsRight() {
boundsRight.set((int) x + 4, (int) y + 35, (int) x + 28, (int) y + 67);
return boundsRight;
}
@Override
public Rect getBoundsLeft() {
boundsLeft.set((int) x + 4, (int) y + 35, (int) x + 28, (int) y + 67);
return boundsLeft;
}
@Override
public Rect getBoundsTop() {
boundsTop.set((int) x + 4, (int) y + 35, (int) x + 28, (int) y + 67);
return boundsTop;
}
public Rect getBoundsMiddle() {
boundsMiddle.set((int) x + 4, (int) y + 35, (int) x + 28, (int) y + 67);
return boundsMiddle;
}
}
and this is the error I get
java.lang.NullPointerException
at com.shuster.killtheex.objects.Ex.die
at com.shuster.killtheex.objects.Ex.update(Ex.java:100)
at com.shuster.killtheex.state.PlayState.update(PlayState.java:48)
at com.shuster.killtheex.framework.GameView.update(GameView.java:73)
at com.shuster.killtheex.framework.GameView.run(GameView.java:119)
at java.lang.Thread.run(Thread.java:841)
Sorry for the long code, I went threw it like 100 times and coudnt fint out why I it doesnt work so maybe someone here could find it.
Thanks alot!!