-1
\$\begingroup\$

Ok. I get this error:

Exception in thread "Thread-2" java.lang.NullPointerException
at basic.game.here.Player.collision(Player.java:42)
at basic.game.here.Player.tick(Player.java:23)
at basic.game.here.Game.tick(Game.java:69)
at basic.game.here.Game.run(Game.java:52)
at java.lang.Thread.run(Unknown Source)

This is the necessary code:

package basic.game.here;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;

public static final int WIDTH = 960, HEIGHT = WIDTH / 12 * 9;

private Thread thread;

private boolean running = false;

private int FPS = 0;

private Player player;
Rectangle ground = new Rectangle(500, HEIGHT - 61, 32, 32);

public Game(){
    player = new Player(500, 200);

    new Window(WIDTH, HEIGHT, "BASIC", this);

    this.addKeyListener(new KeyInput(player));
}

public synchronized void start(){
    thread = new Thread(this);
    running = true;
    thread.start();
}

public synchronized void stop(){
    try{
        thread.join();
        running = false;
    }catch(Exception e){
        e.printStackTrace();
    }
}

public void run() {
    long lastTimeChecked = System.nanoTime();
    int frames = 0;
    while(running){
        tick();
        render();
        try {
            Thread.sleep(6);
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        frames++;
        if(System.nanoTime() - lastTimeChecked >= 1000000000){
            FPS = frames;
            frames = 0;
            lastTimeChecked = System.nanoTime();
    }
}
}

private void tick() {
    player.tick();
}

private void render(){
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null){
        this.createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, WIDTH, HEIGHT);

    g.setColor(Color.YELLOW);
    g.setFont(new Font("Dialog", Font.BOLD, 18));
    g.drawString("" + FPS, 5, 20);

    g.drawString("(" + player.x + ", " + player.y + ")", 5, 40);

    g.setColor(Color.GRAY);
    g.fillRect(ground.x, ground.y, ground.width, ground.height);

    player.render(g);

    g.dispose();
    bs.show();
}

public static void main(String[] args){
    new Game();
}

}

AND:

package basic.game.here;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Player {

public float x, y;
public float velX = 0, velY = 0;
private float gravity = .05f;
private final int MAX_SPEED = 10;
private Game game;
Rectangle playerr;

public Player(float x, float y){
    this.x = x;
    this.y = y;
    playerr = new Rectangle(500, 100, 32, 32);
}

public void tick(){
    if(!collision()){
        playerr.x += velX;
        playerr.y += velY;
    }

    velY += gravity;
    if(velY >= MAX_SPEED){
        velY = MAX_SPEED;
    }

}

public void render(Graphics g){
     g.setColor(Color.CYAN);
     g.fillRect(playerr.x,  playerr.y,  playerr.width,  playerr.height);
}

public boolean collision() {
    boolean solid = false;
    if(game.ground.intersects(playerr)){
        solid = true;
    }
    return solid;
}

public float getVelX() {
    return velX;
}

public void setVelX(float velX) {
    this.velX = velX;
}

public float getVelY() {
    return velY;
}

public void setVelY(float velY) {
    this.velY = velY;
}

}

THANKS IN ADVANCE!!!

\$\endgroup\$
4
  • \$\begingroup\$ Which ones are the lines mentioned in the stack trace? \$\endgroup\$
    – Philipp
    Mar 22, 2017 at 23:21
  • 1
    \$\begingroup\$ Before you post comments for every single line, please edit your question. \$\endgroup\$
    – Philipp
    Mar 22, 2017 at 23:24
  • \$\begingroup\$ Gotcha. Tried to word question best I could. \$\endgroup\$
    – TJRelytS
    Mar 22, 2017 at 23:27
  • \$\begingroup\$ Basically trying to get collision detection between two rectangles.... But I continuously get this error. \$\endgroup\$
    – TJRelytS
    Mar 22, 2017 at 23:28

1 Answer 1

0
\$\begingroup\$

The variable private Game game in the class Player is never initialized, so it is null. That's why game.ground.intersects fails with a null pointer exception in Player.collision. Your IDE should actually give you a warning about that.

A good place to pass the game to the player would be in the constructor of Game when you create it.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks. I did not realize that. I am sure this will work. \$\endgroup\$
    – TJRelytS
    Mar 22, 2017 at 23:36
  • \$\begingroup\$ @TJRelytS You could have found that error yourself by using the method Josh Petrie explained to you in his answer to your last question: By placing a debugger breakpoint in the line which throws the null pointer exception and checking which object is null. Please try to follow the advise we give you. We are not here to find every bug for you. We are here to teach you how to find bugs yourself. \$\endgroup\$
    – Philipp
    Mar 22, 2017 at 23:45
  • \$\begingroup\$ Okay. I apologize. Did not really know what a debugger breakpoint is. \$\endgroup\$
    – TJRelytS
    Mar 23, 2017 at 0:00
  • \$\begingroup\$ @TJRelytS Knowing how to use the debugger of your IDE can save you hours upon hours of bug fixing. I would really recommend you to get familiar with it. It will save you a ton of time and nerves. \$\endgroup\$
    – Philipp
    Mar 23, 2017 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .