Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

JPanel Array -- NullPointerException

Thread view: 
tomzhi - 05 Dec 2007 13:42 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. 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();


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.