title: Why does a Null Pointer Exception occur when I call pack() from JFrame(Swing)?
body: <h1>Precautions taken before asking this question</h1>
  
  <p>I made sure that I initialized all the multidimensional <code>ArrayList</code> objects and their elements with valid and correct parameters to the best of my knowledge. I added the component subclasses(<code>JRadioButton</code>, <code>JLabel</code>) from their <code>ArrayList</code> to their designated <code>JPanel</code> which is in an <code>ArrayList</code>. I set the layout of each of the <code>JPanel</code>s to their own <code>GroupLayout</code> object from an <code>ArrayList</code> and called the necessary methods to set up said <code>GroupLayout</code> object. I added the <code>JPanels</code> to a <code>JFrame</code> with a <code>CardLayout</code> then used <code>pack()</code>. Finally I set the <code>JFrame</code> visible.</p>
  
  <h3>Noteworthy information</h3>
  
  <p>When I comment out the buttons there's no runtime errors.</p>
  
  <h1>SRC</h1>
  
  <pre><code>public class Main{
      public static void main(String[] args)throws java.io.FileNotFoundException{
              JFrame mainFrame = new JFrame();
              /*//Use later for Menu.
              CardLayout menuPages = new CardLayout();*/
              CardLayout pages = new CardLayout(); 
              //Only Using element 2 index 1 for now.
              JPanel[] Menu = new JPanel[3];
              ArrayList&lt;JPanel&gt; cards = new ArrayList&lt;JPanel&gt;();
              ArrayList&lt;ArrayList&lt;ButtonGroup&gt;&gt; Arrlst_ButtonGroup = new ArrayList&lt;ArrayList&lt;ButtonGroup&gt;&gt;();
              ArrayList&lt;ArrayList&lt;JRadioButton[]&gt;&gt; Arrlst_Button = new ArrayList&lt;ArrayList&lt;JRadioButton[]&gt;&gt;();
              JRadioButton[] Buttons = {new JRadioButton("Safe",false), new JRadioButton("Unsafe",false), new JRadioButton("Unobserved",false)};
              ArrayList&lt;ArrayList&lt;JLabel&gt;&gt; Arrlst_JLabel = new ArrayList&lt;ArrayList&lt;JLabel&gt;&gt;();
              ArrayList&lt;JLabel&gt; Arrlst_Sections = new ArrayList&lt;JLabel&gt;();
              Scanner scan = new Scanner(new File("./Questions.txt"));
              Pattern p = Pattern.compile("[^:]+:");
              String line = "";
              for(int counter = -1,count = 0;scan.hasNext();counter+=0){
                  line = scan.nextLine();
                  Matcher m = p.matcher(line);
                  if(m.matches()){
                      counter++;
                      count = 0;
                      //Test Line
                      //System.out.println("Making new JLabel titled:"+line);
                      Arrlst_Sections.add(new JLabel(line));
                      Arrlst_ButtonGroup.add(new ArrayList&lt;ButtonGroup&gt;());
                      Arrlst_Button.add(new ArrayList&lt;JRadioButton[]&gt;());
                      Arrlst_JLabel.add(new ArrayList&lt;JLabel&gt;());
                      //Test line
                      //System.out.println("Label -&gt;"+line);
                  }
                  else {
                      Arrlst_JLabel.get(counter).add(new JLabel(line));
                      Arrlst_Button.get(counter).add(Buttons);
                      ButtonGroup tmp = new ButtonGroup();
                      for(JRadioButton button : Arrlst_Button.get(counter).get(count)){
                          tmp.add(button);
                      }
                      Arrlst_ButtonGroup.get(counter).add(tmp);
                      count++;
                  }
              }
              ArrayList&lt;GroupLayout&gt; Arrlst_GrpLyt = new ArrayList&lt;GroupLayout&gt;();
              for(int count = 0;Arrlst_Sections.size()&gt;count;count++){
                  Arrlst_Sections.get(count).setHorizontalAlignment((int)Arrlst_Sections.get(count).CENTER_ALIGNMENT);
                  cards.add(new JPanel());
                  Arrlst_GrpLyt.add(new GroupLayout(cards.get(count)));
                  Arrlst_GrpLyt.get(count).setAutoCreateContainerGaps(true); 
                  //Test Line
                  //System.out.println("What JLabel may actually say:"+Arrlst_Sections.get(count).getText());
                  GroupLayout.ParallelGroup verticalGroup = Arrlst_GrpLyt.get(count).createParallelGroup();
                  GroupLayout.SequentialGroup verticalJLabel = Arrlst_GrpLyt.get(count).createSequentialGroup();
                  for(int count1 = 0;Arrlst_JLabel.get(count).size()&gt;count1;count1++){
                      verticalJLabel.addComponent(Arrlst_JLabel.get(count).get(count1));
                  }
                  GroupLayout.SequentialGroup verticalJRadioButton = Arrlst_GrpLyt.get(count).createSequentialGroup();
                  //Comment back in when solution for null pointer exception is found.
                  /*Start*/for(int count1 = 0;Arrlst_Button.get(count).size()&gt;count1;count1++){
                      GroupLayout.ParallelGroup tmpVerticalButtonGroup = Arrlst_GrpLyt.get(count).createParallelGroup(GroupLayout.Alignment.TRAILING);
                      for(JRadioButton button : Arrlst_Button.get(count).get(count1)){
                          //Test Line
                          //System.out.println("Count:"+count+" Count1:"+count1);
                          //Test Line
                          //System.out.println(button.getBounds().toString());
                          tmpVerticalButtonGroup.addComponent(button);
                      }
                      verticalJRadioButton.addGroup(tmpVerticalButtonGroup);
                  }/*End*/
                  verticalGroup.addGroup(verticalJLabel);
                  verticalGroup.addGroup(verticalJRadioButton);
                  Arrlst_GrpLyt.get(count).setVerticalGroup(Arrlst_GrpLyt.get(count).createSequentialGroup().addComponent(Arrlst_Sections.get(count)).addGroup(verticalGroup));
                  GroupLayout.SequentialGroup horizontalGroup = Arrlst_GrpLyt.get(count).createSequentialGroup();
                  horizontalGroup.addGroup(Arrlst_GrpLyt.get(count).createParallelGroup(GroupLayout.Alignment.CENTER)).addComponent(Arrlst_Sections.get(count));
                  GroupLayout.ParallelGroup horizontalJLabel = Arrlst_GrpLyt.get(count).createParallelGroup();
                  for(int count1 = 0;Arrlst_JLabel.get(count).size()&gt;count1;count1++){
                      horizontalJLabel.addComponent(Arrlst_JLabel.get(count).get(count1));
                  }
                  horizontalGroup.addGroup(horizontalJLabel);
                  //Comment back in when solution for null pointer exception is found.
                  /*Start*/for(int count1 = 0;3&gt;count1;count1++){
                      GroupLayout.ParallelGroup tmpHorizontalButtonGroup = Arrlst_GrpLyt.get(count).createParallelGroup();
                      for(int count2 = 0;Arrlst_Button.get(count).size()&gt;count2;count2++){
                          tmpHorizontalButtonGroup.addComponent(Arrlst_Button.get(count).get(count2)[count1]);
                      }
                      horizontalGroup.addGroup(tmpHorizontalButtonGroup);
                  }/*End*/
                  Arrlst_GrpLyt.get(count).setHorizontalGroup(horizontalGroup);
                  cards.get(count).setLayout(Arrlst_GrpLyt.get(count));
                  cards.get(count).add(Arrlst_Sections.get(count));
                  for(int count1 = 0;Arrlst_JLabel.get(count).size()&gt;count1;count1++){
                      cards.get(count).add(Arrlst_JLabel.get(count).get(count1));
                      //Comment back in when solution for null pointer exception is found.
                      /*Start*/for(JRadioButton button : Arrlst_Button.get(count).get(count1)){
                          cards.get(count).add(button);
                      }/*End*/
                  }
                  //Not sure what to put for constraints
                  pages.addLayoutComponent(cards.get(count),Arrlst_GrpLyt.get(count).toString());
              }
              Menu[1] = new JPanel(pages);
              for(int count = 0;cards.size()&gt;count;count++){
                  Menu[1].add(cards.get(count));
              }
              mainFrame.add(Menu[1]);
              mainFrame.setDefaultLookAndFeelDecorated(true);
              mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              mainFrame.pack();
              mainFrame.setVisible(true);
              pages.first(Menu[1]);
          }
      }
  </code></pre>
  
  <h1>Runtime error</h1>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at javax.swing.GroupLayout$ComponentInfo.setBounds(Unknown Source)
      at javax.swing.GroupLayout.layoutContainer(Unknown Source)
      at java.awt.Container.layout(Unknown Source)
      at java.awt.Container.doLayout(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validate(Unknown Source)
      at java.awt.Container.validateUnconditionally(Unknown Source)
      at java.awt.Window.pack(Unknown Source)
      at EndUser.Main.main(Main.java:122)
  Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at javax.swing.GroupLayout$ComponentInfo.setBounds(Unknown Source)
      at javax.swing.GroupLayout.layoutContainer(Unknown Source)
      at java.awt.Container.layout(Unknown Source)
      at java.awt.Container.doLayout(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validate(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$500(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
  </code></pre>