簡體   English   中英

JLabel數組setIcon()導致Null指針異常

[英]JLabel array setIcon() cause Null Pointer Exception

我正在寫紙牌游戲,需要用用戶選擇的紙牌替換現有紙牌。 首先,我在面板中顯示了14張卡,這是在我的GUI類中聲明並初始化為以下內容的JLabel數組:

public JLabel[] cardsLabel;  //as class variable
cardsLabel = new JLabel[14];  //inside constructor

並且卡片顯示在下面的代碼面板中,可以很好地工作。

for (int index=0; index<cardsLabel.length; index++)
        {
            cardsLabel[index] = new JLabel();
            cardsLabel[index].setIcon(backOfCard);
            cardsContainer1.add(cardsLabel[index]);
            game.add(cardsContainer1, BorderLayout.EAST);       //add cards to east of game container
        }

我有另一個游戲類,將從其中將用戶選擇索引傳遞給我的GUI類中的方法

public void cardFlip(int c1, int c2) {

        ImageIcon cardOne = new ImageIcon(resizeImage(new ImageIcon(table.getCard(c1).getImg()),100,120));
        ImageIcon cardTwo = new ImageIcon(resizeImage(new ImageIcon(table.getCard(c2).getImg()),100,120));

        cardsLabel[c1].setIcon(cardOne);  //this is where I'm getting Null Pointer Exception error
        cardsLabel[c2].setIcon(cardTwo);
}

我正在使用JLabel數組獲取Null Point Error。 這是前幾行錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at game.GameTableFrame.cardFlip(GameTableFrame.java:162)
at game.FishingGame.gamePlay(FishingGame.java:80)
at game.GameTableFrame.<init>(GameTableFrame.java:152)
at game.GameModule$1.actionPerformed(GameModule.java:95)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

因此,我一直在閱讀其他文章,這可能是由於未正確初始化數組而導致的,所以我無法弄清楚是什么原因導致了此錯誤。 如果有任何一種靈魂可以啟發我,請多多關照。

提前致謝。

更新:

游戲類

public FishingGame(Player p) {

        player = p;
        dealer = new Dealer();
        table = new Table();
        gameTable = new GameTableFrame();     //this is my GUI class where cardFlip is 


        players = new ArrayList<PlayerInterface>();
        players.add(player);
        players.add(dealer);
    }

public void gamePlay() {


        int turn = 0;
        PlayerInterface currentPlayer;

        while (table.hasMoreCard()) { // play the game while the table has cards

            currentPlayer = players.get(turn);
            System.out.println(currentPlayer.getLoginName() + "'s turn");


            // prompt for card numbers
            int[] cardsIndex = currentPlayer.getCardsIndex();

            // show cards on table
            table.showCards(cardsIndex[0], cardsIndex[1]);

            gameTable.cardFlip(cardsIndex[0],cardsIndex[1]);  //this is how I call cardFlip

            // more lines of code below for game
            }

更多更新:

這是cardsLabel初始化的地方。

    public GameTableFrame(Dealer dealer, Player player)
        {

            fg = new FishingGame(player);
            playersData = new PlayersData();
            dealer = new Dealer();
            card = new Card();

            cardsLabel = new JLabel[14];
for (int index=0; index<cardsLabel.length; index++)
        {
            cardsLabel[index] = new JLabel();
            cardsLabel[index].setIcon(backOfCard);
            cardsContainer1.add(cardsLabel[index]);
            game.add(cardsContainer1, BorderLayout.EAST);       //add cards to east of game container
        }

    }

更新:

使用參數化的構造函數,我的程序將在登錄時掛起。 這是我的程序流程:

  1. 登錄(類:GameModule)
  2. 如果成功,將顯示帶有14張卡片的GameFrame(類:GameModuleFrame)
  3. 提示用戶輸入索引號(類:FishingGame)並發送到GameModuleFrame下的CardFlip
  4. 替換用戶選擇的卡(類:GameModuleFrame)**這是我出錯的地方,但是當我更改為參數化構造函數時,我的程序將掛在項目1上。

更新:

變成

public FishingGame(Player p) {

        player = p;
        dealer = new Dealer();
        table = new Table();
        gameTable = new GameTableFrame(dealer,player, this);
}

 public GameTableFrame(Dealer dealer, Player player, FishingGame fg)
    {

        fg = new FishingGame(player);
        playersData = new PlayersData();

// more codes here
}

請問我該如何更改GameModule類?

public GameModule() {
        playersData = new PlayersData();    
        dealer = new Dealer();
        login = new JPanel();
        loginFrame = new JFrame();
    }

//當前這是我從GameModule類中調用GameTableFrame的方式

gameTableFrame = new GameTableFrame(dealer, player);

您必須更改:

gameTable = new GameTableFrame();   

對於:

gameTable = new GameTableFrame(dealer, player);

否則,您將不會初始化卡。

更新

它凍結,因為您正在交叉引用對象。 因此,當您創建GameTableFrame時,它會創建一個FishingGame,后者會創建另一個GameTableFrame等。您不能這樣做。 如果需要引用FishingGame,則應將對象傳遞給GameTableFrame,例如:

new GameTableFrame(dealer, player, this)

並更改:

public GameTableFrame(Dealer dealer, Player player)

對於:

public GameTableFrame(Dealer dealer, Player player, FishingGame fg)

您正在調用錯誤的構造函數。

public GameTableFrame()

初始化在參數化構造函數中完成。

public GameTableFrame(Dealer dealer, Player player)

使用參數化的。

如果索引錯誤,將拋出ArrayIndexOutOfBounds。 我猜cardsLabel數組在代碼的其他地方被更新為null,這就是為什么在訪問NullPointerException時會拋出NullPointerException的原因。

您能否發布您的GUI類完整代碼?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM