# Thread: How to solve the NullPointerException ?

1. Member
Join Date
Aug 2014
Posts
4
Rep Power
0

## How to solve the NullPointerException ?

Hi, I'm creating a simple game in Java and now I'm getting this error :

at Cat.update(Cat.java:40)
at StartingClass.run(StartingClass.java:149)

I read it's the most common error and also found some articles how to solve it, but none of them actually helped me.

This is the code :

public class Cat {
public float OFFSET = 0, MAXSPEED = 15, SPEEDBOOST = 3, GROUND = 425,
DECAYADD = 10, DECAYMUL = 2f/3f, JUMPSPEED = -15;

public float centerX = 100;
public float centerY = GROUND;
public boolean jumped = false;
public boolean alternate = false;
public int seconds;

public static Background bg1 = StartingClass.getBg1();
public static Background bg2 = StartingClass.getBg2();

public static Rain raining = StartingClass.getRaining();

public float speedX = 0;
public float speedY = 1;

public void update() {
speedX *= Math.pow(DECAYMUL, 1 / 60f);

speedX = speedX < 0 ? 0 : speedX;
speedX = speedX > MAXSPEED ? MAXSPEED : speedX;

centerY += speedY;

if (speedX == 0 || speedX < 0) {
bg1.speedX = 0;
bg2.speedX = 0;
}
if (centerX <= 200 && speedX > 0) {
centerX += speedX;
}
if (centerX > 200){
centerX = 200;
bg1.speedX = - speedX;
bg2.speedX = - speedX;
raining.speedX = - speedX;
}
if (centerY + speedY >= GROUND) {
centerY = GROUND;
}

if (jumped == true) {
speedY += 1;

if (centerY + speedY >= GROUND) {
centerY = GROUND;
speedY = 0;
jumped = false;
}

}

if (centerX + speedX <= 60) {
centerX = 61;
}
}

public void moveRight() {
if (alternate == false) {
centerX += OFFSET;
speedX += SPEEDBOOST;
alternate = true;
}
}

public void moveLeft() {
if (alternate == true) {
centerX += OFFSET;
speedX += SPEEDBOOST;
alternate = false;
}
}

public void stopRight() {
}

public void stopLeft() {
}

public void jump() {
if (jumped == false) {
speedY = JUMPSPEED;
jumped = true;
}

}

I will appraciate any help !

2. Member
Join Date
Aug 2014
Posts
4
Rep Power
0

## Re: How to solve the NullPointerException ?

I don't know how to delete this thread, but never mind, already solved.

3. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: How to solve the NullPointerException ?

You can't, threads you create are public domain; its not yours to delete. A mod might do it.

To be brutally honest, you're being a bit selfish. Forums work best when you enter them in an effort to collaborate as its all voluntarily, and that also works its way in the questions you ask; you don't ask a question for yourself (selfish), you ask it for yourself and others. It may be a problem you are having in your particular program, but the solution to the problem might also help others in a similar situation.

Yet here you are, just stating you solved it without explaining how you solved it. You made it all about you.

4. Member
Join Date
Aug 2014
Posts
4
Rep Power
0

## Re: How to solve the NullPointerException ?

I didn't want it to sound like that, I just wanted to make sure noone will take effort to read it and try to find solution when I already found it. I'm sorry about that. The problem was in different class, I chose wrong order of lines. I just had to switch order of creating new objects.

5. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: How to solve the NullPointerException ?

I'm not judging, only informing. If you play it the social way, you'll have a better experience in most forums.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•