Java java.lang.Nullpointerexception in a Jpanel constructor

Asked

Viewed 108 times

0

I’m trying to compile a memory game in java, but gives the following error:

Exception in thread "main" java.lang.NullPointerException

at javax.swing.ImageIcon.<init>(ImageIcon.java:217)

at memorygame.Game.initSquares(Game.java:37)

at memorygame.Game.<init>(Game.java:24)

at memorygame.Main.<init>(Main.java:21)

at memorygame.Main.main(Main.java:33)

I checked these lines with the debug and did not identify the error, below the game class and main, if necessary put the others. Game class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Queue;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import utils.Generate;

public class Game extends JPanel implements Valida {
    private static final long serialVersionUID = 1L;
    private Square[][] squares;
    private LogicaGame logic;

    public Game() {
        setSize(400, 405);
        squares = new Square[4][4];
        logic = new LogicaGame(this, squares);
        addMouseListener(logic);
        initSquares();

    }


    private void initSquares() {
        Queue<Integer> generate = Generate.randomSequence(8);
        Generate.randomSequence(generate, 8);
        int pos = 0;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                Square s = new Square();
                pos = generate.remove();
                ImageIcon ii = new ImageIcon(this.getClass().getResource(
                        "images/" + pos + ".png"));
                s.setImage(ii.getImage());
                s.setPos(pos);
                s.setX(j * 100);
                s.setY(i * 100);
                s.setMyX(i);
                s.setMyY(j);
                squares[i][j] = s;
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(new Color(0, 0, 0));

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                Square s = squares[i][j];
                g2.drawImage(s.getImage(), s.getX(), s.getY(), this);
            }
        }
    }

    @Override
    public boolean valida() {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (!squares[i][j].isBlock()) {
                    return false;
                }
            }
        }
        return true;
    }

}

Main class

    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;

    public class Main extends JFrame {
    private static final long serialVersionUID = 1L;
    private Game game;

    public Game getGame() {
        return game;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    public Main() {
        game = new Game();
        add(game);
        setBackground(new Color(238, 238, 238));
        setSize(410, 430);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
        setTitle(":D");
    }

    public static void main(String[] args) {
        final Main main = new Main();
        new Thread() {

            @Override
            public void run() {
                while (true) {
                    if (main.getGame().valida())
                        break;
                }
                JOptionPane.showMessageDialog(main, "Ganhou!");
                main.hide();
            }

        }.start();

    }

}

1 answer

1


According to the stacktrace you posted, the exception is occurring in the constructor of ImageIcon, in the method initSquares:

ImageIcon ii = new ImageIcon(this.getClass().getResource("images/" + pos + ".png"));

When the method getResource does not find a resource, it returns null. What is probably caused by the NullPointerException.

This way make sure that the resource indicated by the string "images/" + pos + ".png" exists.

  • 1

    Desta forma certifique-se que o recurso indicado pela string "images/" + pos + ".png" existe. - And that it’s in the same package as the class that’s calling it too.

  • Thanks, after putting the images in the same package, because it was in another, it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.