I'm getting this error on this code:
super("Trace Masker");
setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
label1 = new JLabel("Source directory:");
label2 = new JLabel("Target directory:");
label3 = new JLabel("Defect number:");
label4 = new JLabel("Slice tokens:");
label4.setToolTipText("Seperate multiple tokens with comma");
txtSourceDirectory = new JTextField(30);
txtTargetDirectory = new JTextField(30);
txtDefectNumber = new JTextField(30);
txtSliceTokens = new JTextField(30);
btnBrowseSourceDirectory = new JButton("...");
btnBrowseTargetDirectory = new JButton("...");
btnStart = new JButton("Start");
btnCancel = new JButton("Cancel");
pnlLabels = new JPanel(new BoxLayout(pnlLabels, BoxLayout.PAGE_AXIS));
pnlText = new JPanel(new BoxLayout(pnlText, BoxLayout.PAGE_AXIS));
pnlBrowseButtons = new JPanel(new BoxLayout(pnlBrowseButtons, BoxLayout.PAGE_AXIS));
pnlTop = new JPanel(new BoxLayout(pnlTop, BoxLayout.LINE_AXIS));
pnlActionButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
pnlLabels.add(label1);
pnlLabels.add(label2);
pnlLabels.add(label3);
pnlLabels.add(label4);
pnlText.add(txtSourceDirectory);
pnlText.add(txtTargetDirectory);
pnlText.add(txtDefectNumber);
pnlText.add(txtSliceTokens);
pnlBrowseButtons.add(btnBrowseSourceDirectory);
pnlBrowseButtons.add(btnBrowseTargetDirectory);
pnlTop.add(pnlLabels);
pnlTop.add(pnlText);
pnlTop.add(pnlBrowseButtons);
pnlActionButtons.add(btnStart);
pnlActionButtons开发者_Go百科.add(btnCancel);
add(pnlTop);
add(pnlActionButtons);
The error is on this line:
pnlLabels.add(label1);
Just to check if this related specifically to pnlLabels, I commented all of it's lines. The error then happens on:
pnlText.add(txtSourceDirectory);
I've already checked the other 2 questions here about this and fixed the setLayout declaration for the JFrame: Question1 Question2
Your problem comes from the following line (and all other lines looking the same):
pnlLabels = new JPanel(new BoxLayout(pnlLabels, BoxLayout.PAGE_AXIS));
When new BoxLayout(...)
is called, pnlLabels
is still null
since it is not assigned yet. The correct way to do it is in two steps:
pnlLabels = new JPanel();
pnlLabels.setLayout(new BoxLayout(pnlLabels, BoxLayout.PAGE_AXIS);
The problem should disappear (provided you do that for all other code lines similar to that one).
精彩评论