I am new to Java, learning GUI. I am trying to create a TabbedPane
with 10 tabs, by using array of JPanel. However, JPanel arrays are
always null. I do not understand why they are null when they are
arrays but not otherwise. I would appreciate explanation and help on
this code. Thanks
public static void main(String[] args) {
JTabbedPane tab = new JTabbedPane();
int i;
JTextField[] text = new JTextField[10]; //
textfield to add to each panel
JPanel [] panel = new JPanel[10]; //array
of panel
//name of each tab
String [] name = {"One",
"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
for (i=1;i<10;i++) {
panel[i].add(text[i]); //
NullPointerException here
tab.addTab(name[i],panel[i]);
}
System.out.println(tab);
JFrame frame = new JFrame("Testing");
frame.add(tab);
frame.setSize(500,500);
frame.setVisible(true);
}
Chris Dollin - 05 Dec 2007 13:56 GMT
> I am new to Java, learning GUI. I am trying to create a TabbedPane
> with 10 tabs, by using array of JPanel. However, JPanel arrays are
> always null. I do not understand why they are null when they are
> arrays but not otherwise.
When you create an array of references using `new ClassName[N]`,
each element of that array is null. If you want to put something
interesting into the elements, you have to do so yourself.
[It's null because there's no [general] way of knowing how to construct
an element to go into that slot.]

Signature
Chris "loopy" Dollin
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
tomzhi - 05 Dec 2007 17:46 GMT
> > I am new to Java, learning GUI. I am trying to create a TabbedPane
> > with 10 tabs, by using array of JPanel. However, JPanel arrays are
[quoted text clipped - 13 lines]
> Hewlett-Packard Limited Cain Road, Bracknell, registered no:
> registered office: Berks RG12 1HN 690597 England
Chris, but how do I go about doing it?
panel[i] =?
the right said need to be a JPanel too.
tomzhi - 05 Dec 2007 18:14 GMT
Thanks. I figured out that I just needed to add panel[i]=new JPanel()
and box[i]=new JComboBox();