0
While I was doing a course of games for java, I was trying to load a Sprite/image and the following error appeared on the console
Exception in thread "main" java.lang.NullPointerException at DracCastle.Cenarie.getSprite(Cenarie.java:24)
I have two classes, this is the code of the first:
package DracCastle;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import DracCastle.TheGame;
public class TheGame extends Canvas implements Runnable{
public static JFrame frame;
private boolean isRunning = true;
private Thread thread;
private final int WIDTH = 1280;
private final int HEIGHT = 720;
private final int SCALE = 1;
private Cenarie skynight;
private BufferedImage background;
public TheGame(){
skynight = new Cenarie("/SkyNight.png");
background = skynight.getSprite(0, 0, 1280, 720);
setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
initFrame();
}
public void initFrame() {
frame = new JFrame("Dracula's Castle");
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[]) {
TheGame game = new TheGame();
game.start();
}
public void tick() {
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = background.getGraphics();
g.drawImage(background,0,0,null);
g.dispose();
}
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;
}
}}}
This is the code of the second:
package DracCastle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Cenarie{
public Cenarie(String string) {
}
private BufferedImage SkyNight;
public void Skynight (String path) {
try {
SkyNight = ImageIO.read(getClass().getResource(path));
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getSprite(int x,int y,int width,int height) {
return SkyNight.getSubimage(0, 0, 1280, 720);
}}
Your code is not reproducible. How can we help you if we can’t even simulate your problem? I recommend you go to the link below to learn how to create a [mcve] and so we can help you.
– user28595
SkyNight
is null. Create a global variable in the class Cenarie and, before executing the methodgetSubimage
, check whether or not the variable is null.– Valdeir Psr