Whenever I try to add an image to my JPanel I get an NPE. (The error is when I initialize a new ImageIcon!)
What I've tried:
- Class loader
- Main class's get resource
- '/' before path and without '/' before path
Code in main class:
public class Main {
public static void main(String[] args) {
GameWindow game = new GameWindow();
game.setSize(1280, 720);
game.center();
game.addImage(new ImageIcon(Main.class.getResource("player.png")).getImage(), 0, 0);
game.setVisible(true);
}
}
Inside the engine GameWindow class:
public class GameWindow {
private final JFrame frame = new JFrame("Game Window");
private final JPanel board = new JPanel();
public GameWindow() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addImage(Image image, int x, int y) {
board.getGraphics().drawImage(image, x, y, null);
}
public void center() {
frame.setLocationRelativeTo(null);
}
public void setVisible(boolean visible) {
if (visible) {
if (!board.isVisible()) {
board.setVisible(true);
frame.add(board);
}
}
frame.setVisible(visible);
}
public void setSize(int x, int y) {
frame.setSize(x, y);
}
}
Here's what the file path looks like before building it into a JAR and running it.
Any help would be appreciated, I've spent so long (1 day + 1/2) trying to find an answer.