• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with GroupLayout Manager

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i was writing the code for application. So the file Compilation is alright without errors. Actually i want to arrange the text fields using GroupLayout.
But when i execute that file i am getting the following error from JCreator.
Exception in thread "main" java.lang.NullPointerException
at exp4.<init>(exp4.java:26)
at exp4.main(exp4.java:57)
Press any key to continue...


And the code of file is as follows

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;

public class exp4 extends JFrame
{
private CardLayout cl;
private GroupLayout gl;

public exp4()
{
super("Experiment4");
JPanel cp = new JPanel();
cl = new CardLayout();
cp.setLayout(cl);
Container c = getContentPane();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
jp1.add(new JLabel("Name"));
jp2.add(new JLabel ("Card 2"));
jp3.add(new JLabel ("Card 3"));
JTextField tf = new JTextField();
jp1.setLayout(gl);
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(tf)
.addGroup(gl.createParallelGroup(LEADING)
.addComponent(tf)
.addGroup(gl.createSequentialGroup()
.addGroup(gl.createParallelGroup(LEADING)
.addComponent(tf)
))));


cp.add(jp1,"1");
cp.add(jp2,"2");
cp.add(jp3,"3");
JPanel bp = new JPanel();
JButton first = new JButton ("First");
JButton next = new JButton ("Next");
JButton prev = new JButton ("Previous");
JButton last = new JButton ("Last");
bp.add(first);
bp.add(next);
bp.add(prev);
bp.add(last);
getContentPane().add(cp, BorderLayout.NORTH);
getContentPane().add(bp, BorderLayout.SOUTH);
setSize(400,400);
setVisible(true);
}

public static void main(String[] args) {
exp4 exp = new exp4();
exp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exp.setVisible(true);
}
}

Please somebody help me.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to go through the standard procedure for finding the NullPointerException (NPE), remembering that the stack trace will probably tell you which line to look at. I have written about how I find NPEs previously, here, parts of this thread, here, here, and lost of other places. Please look at those threads, then work out which object you have mistakenly got null.
 
Rob Spoor
Sheriff
Posts: 20821
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1: look at line 26 of your class. That's the "gl.setAutoCreateGaps(true);" line. Then ask yourself: where have you initialized g1?
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
prime, i have no where used g1 in the program. Moreover
i have added this code before the Line where this error is caused.
System.err.printf("gl = %s%n",gl);
Fine it works it is showing as gl=null.
But i am sill unable to correct it, i have never declared gl as null. I have also tried with other links which were given before.Can any body please help.
Here is the code with the line i have added.

quote:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;

public class exp4 extends JFrame
{
private CardLayout cl;
private GroupLayout gl;

public exp4()
{
super("Experiment4");
JPanel cp = new JPanel();
cl = new CardLayout();
cp.setLayout(cl);
Container c = getContentPane();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
jp1.add(new JLabel("Name"));
jp2.add(new JLabel ("Card 2"));
jp3.add(new JLabel ("Card 3"));
JTextField tf = new JTextField();
jp1.setLayout(gl);
System.err.printf("gl = %s%n",gl);
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(tf)
.addGroup(gl.createParallelGroup(LEADING)
.addComponent(tf)
.addGroup(gl.createSequentialGroup()
.addGroup(gl.createParallelGroup(LEADING)
.addComponent(tf)
))));
cp.add(jp1,"1");
cp.add(jp2,"2");
cp.add(jp3,"3");
JPanel bp = new JPanel();
JButton first = new JButton ("First");
JButton next = new JButton ("Next");
JButton prev = new JButton ("Previous");
JButton last = new JButton ("Last");
bp.add(first);
bp.add(next);
bp.add(prev);
bp.add(last);
getContentPane().add(cp, BorderLayout.NORTH);
getContentPane().add(bp, BorderLayout.SOUTH);
setSize(400,400);
setVisible(true);
}

public static void main(String[] args) {
exp4 exp = new exp4();
exp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exp.setVisible(true);
}
}
 
Rob Spoor
Sheriff
Posts: 20821
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant gl, sorry about that. But the question remains, where do you initialize it? I don't see any assignment to gl, which means it will remain null.

And will you please UseCodeTags in the future? That will improve the readability of your code.
[ June 02, 2008: Message edited by: Rob Prime ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!