简体   繁体   中英

JLabel array setIcon() cause Null Pointer Exception

I am writing a card game where I need to replace the existing cards with the cards selected by user. First, I have 14 cards displayed in a panel which is a JLabel array declared and initialized in my GUI class as:

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

and the cards are displayed in a panel with the code below which is perfectly working fine.

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
        }

I have another game class from which I am passing user selection index to a method in my GUI class

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);
}

I am getting Null Point Error with the JLabel array. Here's the first few lines of 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)

So I have been reading others posts and it could be due to the array not properly initialized but I have done that so I can't figure out what is causing this error. Would appreciate if any kind soul can enlighten me on this.

Thanks in advance.

Update:

Game class

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
            }

More update:

This is where the cardsLabel is initialised.

    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
        }

    }

Update:

used parameterised constructor and my program will hang at the login. Here's my program flow:

  1. Login (Class: GameModule)
  2. if successful, will show GameFrame with 14 cards (Class: GameModuleFrame)
  3. prompt user to enter index number (Class: FishingGame) and send to CardFlip under GameModuleFrame
  4. replace user selected cards (Class: GameModuleFrame) ** this is where I was having error but when I changed to parameterised constructor, my program will hang at item 1.

Update:

changed to

public FishingGame(Player p) {

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

and

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

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

// more codes here
}

How can i change in GameModule class, please?

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

//currently this is how I call GameTableFrame from GameModule class

gameTableFrame = new GameTableFrame(dealer, player);

You have to change:

gameTable = new GameTableFrame();   

For:

gameTable = new GameTableFrame(dealer, player);

Otherwise, you will not initialize the cards.

UPDATE

It freezes because you are cross referencing objects. So, when you create GameTableFrame, it creates a FishingGame, which creates another GameTableFrame, etc. You cannot do this. If you need to reference FishingGame, you should pass the object to GameTableFrame, like:

new GameTableFrame(dealer, player, this)

And change:

public GameTableFrame(Dealer dealer, Player player)

For:

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

You are calling the wrong constructor.

public GameTableFrame()

Initialization is done in a parameterized constructor.

public GameTableFrame(Dealer dealer, Player player)

Use the parameterised one.

If the index is wrong, ArrayIndexOutOfBounds will be thrown. I guess the cardsLabel array is getting updated as null somewhere else in your code and that is the reason why NullPointerException is thrown when it is accessed.

Could you please post your GUI class full code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM