title: java error when I use my shuffle() but not when I use my randomNum()
body: <p>I had my Lottery game working but I needed to change the way the computer generated the winning numbers.  At first I generated an array of random numbers but i was getting duplicates.  To address this I switch to shuffling an array of 30 numbers and using the first six in the array.  Similar to how the first 6 balls that drop in the lottery are the ones used.  When I run the program it seems to crash at the <code>compareResults()</code>.  In the <code>itemStateChanged(ItemEvent e)</code> I have a call to the <code>randomNum()</code> and the <code>shuffle()</code>. I am not sure why it will not compare the results since I am ending up with the <code>randNum[]</code> regardless
   of what method I use to generate it.</p>
  
  <pre><code>        import java.awt.BorderLayout;
          import java.awt.Color;
          import java.awt.Container;
          import java.awt.FlowLayout;
          import java.awt.Font;
          import java.awt.GridLayout;
          import java.awt.event.ItemEvent;
          import java.awt.event.ItemListener;
          import java.util.Arrays;
          import java.util.Collections;
  
          import javax.swing.JButton;
          import javax.swing.JCheckBox;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JPanel;
  
      public class JLottery2 extends JFrame implements ItemListener {
          private String[] lotteryNumbers = { "1", "2", "3", "4", "5", "6", "7", "8",
          "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
          "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" };
          private JPanel jp1 = new JPanel();
          private JPanel jp2 = new JPanel();
          private JPanel jp3 = new JPanel(new GridLayout(3, 10, 5, 5));
          private JLabel jl1 = new JLabel("The Lottery Game!!!!!");
          private JLabel jl2 = new JLabel(
          "To play, pick six number that match the randomly selected numbers.");
          private FlowLayout layout = new FlowLayout();
          private GridLayout gridBase = new GridLayout(3, 1, 5, 5);
          private GridLayout grid = new GridLayout(3, 10, 5, 5);
          private Font heading = new Font("Palatino Linotype", Font.BOLD, 24);
          private Font bodyText = new Font("Palatino Linotype", Font.BOLD, 14);
          private Color color1 = new Color(4, 217, 225);
          private Color color2 = new Color(4, 225, 129);
          private int maxNumber = 6;
          final int LOTTERY_NUMBER_MAX = 30;
          private int counter = 0;
          private int[] randomNum;
          private int[] userPickedNumbers;
  
          private int matchedNumbers = 0;
          private JCheckBox checkBox[] = new JCheckBox[lotteryNumbers.length];
          private String temp;
          Container con = getContentPane();
  
          public JLottery2() {
      super("The Lottery Game");
      con.setLayout(gridBase);
      con.add(jp1);
      jp1.setLayout(layout);
      jp1.add(jl1);
      jl1.setFont(heading);
      jp1.setBackground(color1);
  
      con.add(jp2);
      jp2.setLayout(layout);
      jp2.add(jl2);
      jl2.setFont(bodyText);
      jp2.setBackground(color1);
  
      con.add(jp3);
      jp3.setLayout(grid);
      for (int i = 0; i &lt; lotteryNumbers.length; i++) {
          // JCheckBox checkBox[] = new JCheckBox[lotteryNumbers.length];
          checkBox[i] = new JCheckBox(lotteryNumbers[i]);
          jp3.add(checkBox[i]);
          jp3.setBackground(color2);
          checkBox[i].addItemListener(this);
      }
  
      setSize(500, 300);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      userPickedNumbers = new int[maxNumber];
  }
  
  public void itemStateChanged(ItemEvent e) {
      /*
       * actions taken as user checks the JCheckBoxes
       */
  
      if (e.getStateChange() == ItemEvent.SELECTED &amp;&amp; counter &lt; maxNumber) {
  
          temp = ((JCheckBox) e.getSource()).getText();
          ((JCheckBox) e.getSource()).setEnabled(false);
          int intTemp = Integer.parseInt(temp);
  
          userPickedNumbers[counter] = intTemp;
          counter++;
          System.out.println("add to counter");
          System.out.println("the .getText() returns "
                  + ((JCheckBox) e.getSource()).getText());
          System.out.println("the tempoaray int is  " + intTemp);
          System.out.println("the number of picks is " + counter);
          System.out.println("**************************************");
      }
      /*
       * actions taken when the user has chosen 6 JCheckboxes
       */
      if (counter == maxNumber) {
          System.out.println("the picks have maxxed out");
          JCheckboxSetVisibleFalse();
          randNumber();
          //shuffle();
          compareResults();
      }
  }
  
  /*
   * creates an array of random lottery numbers
   * array of 30 numbers is created then shuffled. The 
   * First 6 elements are place in the randomNum[]
   */
  public void randNumber() {
  
      randomNum = new int[maxNumber];
      for (int i = 0; i &lt; maxNumber; i++) {
          randomNum[i] = ((int) (Math.random() * 100) % lotteryNumbers.length + 1);
      }
      System.out.println("the randomNum array is "
              + Arrays.toString(randomNum));
              }
  
  
  
  
      public void shuffle(){
      Integer[] randomNumTemp = new Integer[LOTTERY_NUMBER_MAX];
      for (int i = 0; i &lt; LOTTERY_NUMBER_MAX; i++) {
          randomNumTemp[i] = i+1;
      }
      //shuffle numbers
     Collections.shuffle(Arrays.asList(randomNumTemp));
     //add first 6 numbers to the randomNumTemp to randNum array
      Integer[] randomNum = new Integer[maxNumber];
  
      for (int i = 0; i &lt; randomNum.length; i++) {
          randomNum[i] = randomNumTemp[i];
      }
  }
  
  /*
   * compares the userPickedNumbers[] to the randomNum[]
   */
  private void compareResults() {
      System.out.println("the user picks are "
              + Arrays.toString(userPickedNumbers));
      int i = 0;// user
      int j = 0;// random
      do {
          if (userPickedNumbers[i] != randomNum[j]
                  &amp;&amp; j &lt; maxNumber - 1) {
  
              System.out.println("the user picked " + userPickedNumbers[i]
                      + " the random number is " + randomNum[j]);
              System.out.println("wrong guess " + j);
              System.out
                      .println("the location of the element in the fisrt array is "
                              + i);
              System.out
                      .println("the location of the element in the second array is "
                              + j);
              j++;
  
          } else if (userPickedNumbers[i] != randomNum[j] &amp;&amp; j == 5) {
  
              System.out.println(userPickedNumbers[i] + randomNum[j]);
              System.out.println("one last wrong guess " + j);
              System.out.println("the location of the first array is " + i);
              j = 0;
              i++;
  
          } else if (userPickedNumbers[i] == randomNum[j]) {
              matchedNumbers++;
              i++;
              j = 0;
              System.out.println("the number of correct guesses are "
                      + matchedNumbers);
              System.out.println("the value of j is" + j);
              System.out.println("the value of i is" + i);
  
          } else if (randomNum[j] == maxNumber
                  &amp;&amp; userPickedNumbers[i] == randomNum[j]) {
              j = 0;
              i++;
              matchedNumbers++;
          }
  
      } while (i &lt; maxNumber);
  
  }
  
  private void JCheckboxSetVisibleFalse() {
      for (int i = 0; i &lt; lotteryNumbers.length; i++) {
  
          checkBox[i].setVisible(false);
  
      }
  }
  
  public static void main(String[] args) {
      JLottery2 frame = new JLottery2();
      frame.setVisible(true);
  }
  </code></pre>
  
  <p>}</p>