The line that throws the NPE will tell you exactly what's wrong:
temp1Panel.add(container1);
You're trying to use a component, container1, before it has been initialized.
Suggestions:
- Don't do this. Initialize variables first before using them.
- Don't brush off exceptions but instead read them critically and study the lines that throw them as doing so will save you a lot of time in the future.
- If you need help with exceptions in the future, please be sure to post the exception stacktrace, and indicate with an obvious comment
// **** NullPointerException here ****
in your posted code. - Your code is a bit strange. You appear to be adding the contentPane to one of your components -- it should be the other way around. Your code as written if it compiles will show no components.
- And I agree with Sage, GroupLayout is hard to work with for hand coding, but having said that, it's not impossible, and I have used it before.
Edit
- You don't even need the container1 variable as you do nothing with it. In essence, it is messing you up and providing you with no value, so get rid of it.
Your code with some modifications:
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame {
private static final long serialVersionUID = 6666;
private static final int COLS = 10;
private JLabel lblcode;
private JLabel lblemployeedetails;
private JLabel lblname;
private JLabel lbladdress;
private JLabel lblcity;
private JLabel lblemail;
private JLabel lbltelnumber;
private JLabel lblcellnumber;
private JLabel lblprnumber;
private JLabel lblppe;
private JLabel lblppedate;
private JTextField txtprnumber;
private JTextField txtppe;
private JTextField txtcode;
private JTextField txtname;
private JTextField txtaddress;
private JTextField txtaddress2;
private JTextField txtcity;
private JTextField txtemail;
private JTextField txttelnumber;
private JTextField txtcellnumber;
private JPanel mainPanel = new JPanel();
private JPanel temp1Panel = new JPanel();
private JPanel temp2Panel = new JPanel();
private GroupLayout glayout;
public Test() {
// Frame Parameters
setTitle("test");
// !! setSize(500, 500);// Adjust size of the frame/window
// !! setLocationRelativeTo(null);// Adjust open location
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// !! testing = new JLabel("testing");
lblcode = new JLabel("Code:");
lblemployeedetails = new JLabel("Employee Details:");
lblname = new JLabel("Name:");
lbladdress = new JLabel("Address:");
lblcity = new JLabel("City:");
lblemail = new JLabel("Email:");
lbltelnumber = new JLabel("Tel:");
lblcellnumber = new JLabel("Cell:");
lblprnumber = new JLabel("PR#:");
lblppe = new JLabel("PPE:");
lblppedate = new JLabel("PPE Date:");
// !! add COLS column size field
txtprnumber = new JTextField(COLS);
txtppe = new JTextField(COLS);
txtcode = new JTextField(COLS);
txtname = new JTextField(COLS);
txtaddress = new JTextField(COLS);
txtaddress2 = new JTextField(COLS);
txtcity = new JTextField(COLS);
txtemail = new JTextField(COLS);
txttelnumber = new JTextField(COLS);
txtcellnumber = new JTextField(COLS);
temp1Panel = new JPanel();
// add before using:
// !! container1 = getContentPane(); // !!
// !! temp1Panel.add(container1);
temp2Panel = new JPanel();
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(temp1Panel);
mainPanel.add(temp2Panel);
getContentPane().add(mainPanel); // !!
glayout = new GroupLayout(temp1Panel);
temp1Panel.setLayout(glayout);
glayout.setAutoCreateGaps(true);
glayout.setAutoCreateContainerGaps(true);
glayout
.setHorizontalGroup(glayout
.createSequentialGroup()
.addGroup(
glayout
.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(lblprnumber).addComponent(lblcode)
.addComponent(lblname).addComponent(lbladdress)
.addComponent(lblcity).addComponent(lblemail)
.addComponent(lbltelnumber)
.addComponent(lblcellnumber))
.addGroup(
glayout
.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(txtprnumber).addComponent(txtcode)
.addComponent(txtname).addComponent(txtaddress)
.addComponent(txtaddress2).addComponent(txtcity)
.addComponent(txtemail)
.addComponent(txttelnumber)
.addComponent(txtcellnumber))
.addGroup(
glayout.createParallelGroup(
GroupLayout.Alignment.LEADING).addComponent(
lblppe))
.addGroup(
glayout
.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(txtppe)
.addComponent(lblemployeedetails))
.addGroup(
glayout.createParallelGroup(
GroupLayout.Alignment.LEADING).addComponent(
lblppedate)));
glayout.setVerticalGroup(glayout
.createSequentialGroup()
.addGroup(
glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblprnumber).addComponent(txtprnumber)
.addComponent(lblppe).addComponent(txtppe)
.addComponent(lblppedate))
.addGroup(
glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblcode).addComponent(txtcode)
.addComponent(lblemployeedetails))
.addGroup(
glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblname).addComponent(txtname))
.addGroup(
glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lbladdress).addComponent(txtaddress))
.addGroup(
glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(txtaddress2))
.addGroup(
glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblcity).addComponent(txtcity))
.addGroup(
glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblemail).addComponent(txtemail))
.addGroup(
glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lbltelnumber).addComponent(txttelnumber))
.addGroup(
glayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblcellnumber)
.addComponent(txtcellnumber))
);
pack(); // !!
setLocationRelativeTo(null); // !!
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
Edit 2 Regarding your edit to your question,
Would I then do glayout2 = new GroupLayout(temp2Panel); temp2Panel.setLayout(glayout2); glayout2.setAutoCreateGaps(true); glayout2.setAutoCreateContainerGaps(true); To make a second panel with grouplayout and will appear in the same Frame?
Sure, that could work. What happens when you try it?
Edit 3
The exception stacktrace suggests that you're trying to add one GroupLayout to more than one container. But your posted code does not reflect this problem. Please add another edit to the bottom of your latest one, one with the code that produces the exception that you're showing above.