esse é o meu código, que não aponta nenhum erro nas linhas, mas na hora de dar run ele dá erro e diz a seguinte mensagem: Exception in thread “main” java.lang.NullPointerException
at gph.Game.render(Game.java:67)
at gph.Game.run(Game.java:88)
at gph.Game.(Game.java:27)
at gph.Game.main(Game.java:53)
sei que o problema ta na linha 67(Graphics g = image.getGraphics(); ) pois todas as outras linhas estão apontadas pq eu chamo metodos que aplicariam essa linha, alguém sabe qual é o erro?
package gph;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
public static JFrame frame;
private Thread thread;
private boolean isRunning = true;
private final int WIDTH = 160;
private final int HEIGHT = 120;
private final int SCALE = 4;
private BufferedImage image;
public Game() {
initFrame();
run();
image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
}
private void initFrame() {
setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
frame = new JFrame("Game1#");
frame.add(this);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public synchronized void start() {
thread = new Thread(this);
isRunning = true;
thread.start();
}
public synchronized void stop() {
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void tick() {
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics(); // <provavel local do erro
g.setColor(new Color(19,19,19 ));
g.fillRect(0,0,WIDTH,HEIGHT);
g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, WIDTH*SCALE, HEIGHT*SCALE, null);
bs.show();
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000/amountOfTicks;
double delta = 0;
int frames = 0;
double timer = System.currentTimeMillis();
while(isRunning) {
long now = System.nanoTime();
delta+= (now - lastTime) / ns;
lastTime = now;
if (delta >= 1) {
tick();
render();
frames ++;
delta--;
}
if(System.currentTimeMillis() - timer >= 1000) {
System.out.println("FPS:"+ frames);
frames = 0;
timer+=1000;
}
}
}
}