title: Setting an array value to null
body: <p>I am trying to change a value of an array to null.</p>
  
  <p>I have an array called card[100].
  In this array I have I have 100 cards stored. Each card hold two int values and a string value: </p>
  
  <pre><code>public Card(int manaCost, int damageDone, java.lang.String name) {
  </code></pre>
  
  <p>Every array index has been filled but I would like to be able to set a value to null when another method is called. I have tried the following:</p>
  
  <ol>
  <li><code>array[index] = null;</code> - this causes an error</li>
  <li><code>array[index] = new array(null, null, null);</code></li>
  <li>A<code>rray tempArray = null; array[index] = tempArray;</code></li>
  </ol>
  
  <p>I am completely baffled as to how to set an array item to null when I feel like this should be an easy fix. </p>
  
  <p>Here is the full code of this class, the method that I am struggling with is <strong>getRandomCard</strong>: </p>
  
  <pre><code>public class Deck {
  
      private Card[] card = new Card[100];
      private int cardsRunning = 100;
      static int DECKSIZE = 100;
      static java.lang.String emailID = "MITNY013";
      private boolean random = false;
  
      /**
       * Constructor for the Deck. Adds the following cards to the deck in the following order
       * 4x Super Lucky Strike, Damage 100, Mana 2 
       * 6x Mega Santa Hit, Damage 80, Mana 2
       * 10x Critical Hit, Damage 50, Mana 5
       * 10x Massive Strike, Damage 40, Mana 7
       * 15x Wrong Way Down A One Way Street, Damage 30, Mana 10
       * 15x Bender Rules Here, Damage 15, Mana 10
       * 40x Trade, Damage 5, Mana 5 &lt;br&gt;
       * My solution length: 32 lines (space lines not included)
       * @param random - Whether to turn on random features
       */
  
      public Deck(boolean random) {
          if (random == false) {
              for (int index = 0; index &lt;= 3; index++) {
                  card[index] = new Card( 2, 100, "Lucky Strike");}
              for (int index = 4; index &lt;= 10; index++) {
                  card[index] = new Card( 2, 80, "Santa Hit");}
              for (int index = 11; index &lt;= 20; index++) {
                  card[index] = new Card( 5, 50, "Critical Hit");}
              for (int index = 21; index &lt;= 30; index++) {
                  card[index] = new Card ( 7, 40, "Massive Strike");}
              for (int index = 31; index &lt;= 45; index++) {
                  card[index] = new Card( 10, 30, "Wrong Way Down A One Way Street");}
              for (int index = 46; index &lt;= 60; index++) {
                  card[index] = new Card( 10, 15, "Bender Rules Here");} 
              for (int index = 61; index &lt;= cardsRunning-1; index++) {
                  card[index] = new Card( 5, 5, "Trade");} }
          else if (random == true) { 
              for (int index = 0; index &lt;= 3; index++) {
                  card[index] = new Card( 2, 100, "Lucky Strike");}
              for (int index = 4; index &lt;= 10; index++) {
                  card[index] = new Card( 2, 80, "Santa Hit");}
              for (int index = 11; index &lt;= 20; index++) {
                  card[index] = new Card( 5, 50, "Critical Hit");}
              for (int index = 21; index &lt;= 30; index++) {
                  card[index] = new Card ( 7, 40, "Massive Strike");}
              for (int index = 31; index &lt;= 45; index++) {
                  card[index] = new Card( 10, 30, "Wrong Way Down A One Way Street");}
              for (int index = 46; index &lt;= 60; index++) {
                  card[index] = new Card( 10, 15, "Bender Rules Here");} 
              for (int index = 61; index &lt;= cardsRunning-1; index++) {
                  card[index] = new Card( 5, 5, "Trade");}
              shuffle(card);}
      }
  
  
      /**
       * Returns a string representation of the entire deck in the format
       * Deck [ 1:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]
       *        2:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]
       *        3:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]
       *        4:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]] &lt;br&gt;
       * My solution length: 6 lines
       * @overrides - toString in class java.lang.Object
       */
  
      @Override
      public String toString() {
          StringBuilder sb = new StringBuilder();
          for (int index = 0; index &lt; card.length; index++) {
              sb.append((index + 1) + ": " +  card[index].toString() + "\n");}
          return sb.toString();
      }
  
      /**
       * Uses a random number generator to get a card from the deck somewhere then swaps the 
       * last card in the deck to that position and reduces the cardsRemaining by one. 
       * This is important. if the random flag is false you should always get the card at 
       * position 0. When you swap the card out you should also set the old position to null 
       * for safety. &lt;br&gt;
       * My solution length: 6 lines
       * @return - the card drawn or null if no cards left in deck
       */
  
      public Card getRandomCard() {
          if (cardsRunning == 0) {
              return null;
          }
          // use random number generator to generate a number to get a card from the deck
          Random rand = new Random();
          int randomCardNo = rand.nextInt(cardsRunning);
          // created newCard in order to return the original random card as it will be replaced by the last value 
          //in the partially fill array
          Card newCard = card[randomCardNo]; 
          //reorder the array - swaps the last card in the deck to the position of the card being removed
          card[randomCardNo] = card[cardsRunning-1];
          //Set the card[cardsRunning -1] to null value so it is removed form the deck
          card[cardsRunning-1] = null;
          cardsRunning--;
          return newCard;
      }
  
      /**
       * Return the number of cards remaining in the deck.
       * My solution length: 1 lines
       * @return - the total cards remaining
       */
  
      public int getCardsRemaining() {
          return this.cardsRunning;}
  
  
      /**
       * BONUS Method
       * My solution length: 6 lines
       */
      public void shuffle(Card[] card) {
          Random rand = new Random();
          for (int index = 0; index &lt; card.length; index++) {
              int randomIndexToSwap = rand.nextInt(card.length);
              Card temp = card[randomIndexToSwap];
              card[randomIndexToSwap] = card[index];
              card[index] = temp;}
      }
  </code></pre>
  
  <p>As mentioned the error is a NullPointerException, but I cam not sure how to convert card[] to a list given it has 3 variables..hmmm </p>
  
  <p>Thanks for your help,</p>