title: JLabel array setIcon() cause Null Pointer Exception
body: <p>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:</p>
  
  <pre><code>public JLabel[] cardsLabel;  //as class variable
  cardsLabel = new JLabel[14];  //inside constructor
  </code></pre>
  
  <p>and the cards are displayed in a panel with the code below which is perfectly working fine.</p>
  
  <pre><code>for (int index=0; index&lt;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
          }
  </code></pre>
  
  <p>I have another game class from which I am passing user selection index to a method in my GUI class </p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>I am getting Null Point Error with the JLabel array.
  Here's the first few lines of error:</p>
  
  <pre><code>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.&lt;init&gt;(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)
  </code></pre>
  
  <p>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. </p>
  
  <p>Thanks in advance.</p>
  
  <p>Update:</p>
  
  <p>Game class</p>
  
  <pre><code>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&lt;PlayerInterface&gt;();
          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
              }
  </code></pre>
  
  <p>More update:</p>
  
  <p>This is where the cardsLabel is initialised.</p>
  
  <pre><code>    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&lt;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
          }
  
      }
  </code></pre>
  
  <p>Update:</p>
  
  <p>used parameterised constructor and my program will hang at the login.
  Here's my program flow:</p>
  
  <ol>
  <li>Login (Class: GameModule)</li>
  <li>if successful, will show GameFrame with 14 cards (Class: GameModuleFrame)</li>
  <li>prompt user to enter index number (Class: FishingGame) and send to CardFlip under GameModuleFrame</li>
  <li>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. </li>
  </ol>
  
  <p>Update:</p>
  
  <p>changed to</p>
  
  <pre><code>public FishingGame(Player p) {
  
          player = p;
          dealer = new Dealer();
          table = new Table();
          gameTable = new GameTableFrame(dealer,player, this);
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code> public GameTableFrame(Dealer dealer, Player player, FishingGame fg)
      {
  
          fg = new FishingGame(player);
          playersData = new PlayersData();
  
  // more codes here
  }
  </code></pre>
  
  <p>How can i change in GameModule class, please? </p>
  
  <pre><code>public GameModule() {
          playersData = new PlayersData();    
          dealer = new Dealer();
          login = new JPanel();
          loginFrame = new JFrame();
      }
  </code></pre>
  
  <p>//currently this is how I call GameTableFrame from GameModule class</p>
  
  <pre><code>gameTableFrame = new GameTableFrame(dealer, player);
  </code></pre>