如何在线程“main”java.lang.NullPointerException错误中解决异常

时间:2014-10-16 02:56:37

标签: java nullpointerexception

例外是:

Exception in thread “main” java.lang.NullPointerException    
 at javax.swing.ImageIcon.<init>(Unknown Source),

at PlayGame.setCards(PlayGame.java:42)

at PlayGame.startGame(PlayGame.java:190)

at forms.PlayGame.<init>(PlayGame.java:36)

这是我的代码

public class PlayGame extends javax.swing.JFrame {

    private LogicGame logic = new LogicGame();
    private ImageIcon img1;
    private ImageIcon img2;

     // Creates new form PlayGame
    public PlayGame() {
        initComponents();
        startGame();
    }

    private void setCards() {
        int[] numbers = logica.getCardNumbers();

        Carta1.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[0] + ".png")));
        Carta2.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[1] + ".png")));
        Carta3.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[2] + ".png")));
        Carta4.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[3] + ".png")));
        Carta5.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[4] + ".png")));
        Carta6.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[5] + ".png")));
        Carta7.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[6] + ".png")));
        Carta8.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[7] + ".png")));
        Carta9.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[8] + ".png")));
        Carta10.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[9] + ".png")));
        Carta11.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[10] + ".png")));
        Carta12.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[11] + ".png")));
        Carta13.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[12] + ".png")));
        Carta14.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[13] + ".png")));
        Carta15.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[14] + ".png")));
        Carta16.setDisabledIcon(new ImageIcon(getClass().getResource("../img/c" + numbers[15] + ".png")));
    }

    public void startGame() {
        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "Pulse si, para jugar!. ", "Empezar Juego", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)) {
            setCards();
            hour.setVisible(false);
            iniciarTiempo();
        } else {
            System.exit(0);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

1)我没有看到任何名为“logica”的变量,2)但问题是在ure数组[]中,异常告诉你你的调用索引不存在于你的数组中