I made sure that I initialized all the multidimensional ArrayList
objects and their elements with valid and correct parameters to the best of my knowledge. I added the component subclasses(JRadioButton
, JLabel
) from their ArrayList
to their designated JPanel
which is in an ArrayList
. I set the layout of each of the JPanel
s to their own GroupLayout
object from an ArrayList
and called the necessary methods to set up said GroupLayout
object. I added the JPanels
to a JFrame
with a CardLayout
then used pack()
. Finally I set the JFrame
visible.
When I comment out the buttons there's no runtime errors.
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<JPanel> cards = new ArrayList<JPanel>();
ArrayList<ArrayList<ButtonGroup>> Arrlst_ButtonGroup = new ArrayList<ArrayList<ButtonGroup>>();
ArrayList<ArrayList<JRadioButton[]>> Arrlst_Button = new ArrayList<ArrayList<JRadioButton[]>>();
JRadioButton[] Buttons = {new JRadioButton("Safe",false), new JRadioButton("Unsafe",false), new JRadioButton("Unobserved",false)};
ArrayList<ArrayList<JLabel>> Arrlst_JLabel = new ArrayList<ArrayList<JLabel>>();
ArrayList<JLabel> Arrlst_Sections = new ArrayList<JLabel>();
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<ButtonGroup>());
Arrlst_Button.add(new ArrayList<JRadioButton[]>());
Arrlst_JLabel.add(new ArrayList<JLabel>());
//Test line
//System.out.println("Label ->"+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<GroupLayout> Arrlst_GrpLyt = new ArrayList<GroupLayout>();
for(int count = 0;Arrlst_Sections.size()>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()>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()>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()>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>count1;count1++){
GroupLayout.ParallelGroup tmpHorizontalButtonGroup = Arrlst_GrpLyt.get(count).createParallelGroup();
for(int count2 = 0;Arrlst_Button.get(count).size()>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()>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()>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]);
}
}
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)