Results 1 to 5 of 5
  1. #1
    Dommie is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default How to solve the NullPointerException ?

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

    Exception in thread "Thread-3" java.lang.NullPointerException
    at Cat.update(Cat.java:40)
    at StartingClass.run(StartingClass.java:149)
    at java.lang.Thread.run(Unknown Source)

    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 -= DECAYADD / 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. #2
    Dommie is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default Re: How to solve the NullPointerException ?

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

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Dommie is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default 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. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. How to solve java.lang.NullPointerException error?
    By aschroft in forum Advanced Java
    Replies: 6
    Last Post: 12-04-2013, 03:32 PM
  2. I can not solve this "NullPointerException"
    By kumar_ravi in forum New To Java
    Replies: 4
    Last Post: 11-15-2012, 06:16 PM
  3. Please solve my error occured java.lang.NullPointerException
    By Viruthagiri in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-24-2010, 01:08 PM
  4. Please solve my java.lang.NullPointerException error.?
    By Viruthagiri in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-23-2010, 10:30 PM
  5. Plz solve this....
    By theone3nu in forum New To Java
    Replies: 9
    Last Post: 12-23-2008, 10:42 AM

Posting Permissions

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