473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

CardLayout Manager is giving some trouble

26
Hi.

I have been trying to fiddle around with some layout managers, the CardLayout in particular. The idea I had was : I must be able to accept multiple panels on one frame, i.e, i must have the first panel with a navigation button labeled "Next", and on clicking it, i am expected to see another panel on the frame.

In my sample code, i have three classes: CardLayoutSelf, Page0, Page1.

The design is intended to work as follows :

1. The class CardLayoutSelf contains a variable cards of type JPanel. Also, I have a void createAndShowGUI( ) method. This method is responsible for adding the JPanels representing pages 0 and 1 onto the CardLayout, and in turn, adding it to the JFrame.

2. Pages 0 and 1, represented by the classes Page0 and Page1 each contain navigation buttons, "Next" and "Back" which when clicked shall lead to the corresponding page relative to the present page. Each of these two classes implement ActionListener.

3. I have a method JPanel addComponentToPane() in each of the classes Page0 a Page1, which return the JPanel of that particular page. This method is invoked in createAndShowGUI( ),

The panels are being loaded onto the frame's ContenPane successfully. This is the code for it in class Page0 :

Expand|Select|Wrap|Line Numbers
  1.    public void actionPerformed(ActionEvent e)
  2.     {
  3.         String next = " Next ";
  4.         String back = " Back ";
  5.  
  6.         CardLayoutSelf c = new CardLayoutSelf();
  7.         CardLayout cl = (CardLayout)(c.cards.getLayout());
  8.  
  9.         if(next.equalsIgnoreCase(e.getActionCommand()) )
  10.         {
  11.          cl.show(c.cards, "1");
  12.         }
  13.    }
My createAndShowGUI() definition is as follows:

Expand|Select|Wrap|Line Numbers
  1. void createAndShowGUI()
  2.     {
  3.         cards = new JPanel(new CardLayout());
  4.  
  5.         JFrame frame = new JFrame(" CardLayouts");
  6.         frame.setLocation(400,300);
  7.         frame.setSize(600,450); 
  8.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9.  
  10.         //Create and set up the content pane.
  11.         Page0 p0 = new Page0();
  12.         cards.add("0", p0.addComponentToPane());
  13.  
  14.         Page1 p1 = new Page1();
  15.         cards.add("1", p1.addComponentToPane());
  16.  
  17.         frame.getContentPane().add (cards,BorderLayout.CENTER);
  18.  
  19.         //frame.pack();
  20.         frame.setVisible(true);
  21.  
  22.     }
The error I get, on clicking the "Next" button on Page 0 is :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Page0.actionPerformed(CardLayoutSelf.java:140)

Can anyone help me out? It seems to be a problem involving actionPerformed( ), but i simply am not able to pin it down.
Jun 26 '09 #1
5 2316
r035198x
13,262 8TB
With time you will be able to read exception traces and pick up important information from them. The message you got is telling you the exact line number in your code where a problem is happening. the problem that is happening is that you trying to access a variable whose value is null.
Jun 26 '09 #2
p vs np
26
Yes.
I do realise the significance of the line number, and in my code, it corresponds to the statement:

Expand|Select|Wrap|Line Numbers
  1. CardLayout cl = (CardLayout)(c.cards.getLayout());
in the actionPerformed() method.

But I simply do not get as to why/how cl could be receiving a NULL value!?
Jun 26 '09 #3
r035198x
13,262 8TB
If (CardLayout)(c.cards.getLayout()) throws a null pointer, then one of the objects being dereferenced on that line is null.
Candidates are c, c.cards and c.cards.getLayout().
Jun 26 '09 #4
p vs np
26
Silly me.
I had not made cards , the variable in class CardLayoutSelf static.

Thanks for the help :)
Jun 26 '09 #5
r035198x
13,262 8TB
I hope you are using How to use CardLayout as a reference while you are doing this.
Jun 26 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Richard Holliingsworth | last post by:
Hello: Thanks for reading this. I'm running A2002 as a front end to SQL Server 7.0. I've finished building my tables, views, forms and reports. Now I want to build the Switcboard form. But...
2
by: Wayne | last post by:
I've just performed a reformat and reinstalled Access 2000 and 2003 and as usual the linked table manager in Access 2003 is broken ie. it doesn't show any tables. I've gone through the process of...
0
by: news.microsoft.com | last post by:
Greetings, I am building a ASPX UI that will allow a HR representative manage the AD Organization. I have a class that wraps a lot of AD work and I do things like change password and update the...
0
by: sonali_reddy123 | last post by:
Hi all, I am using a crownwood components docking manager in my application.I want to assign a tip to the Auto Hide and close buttons present for the docking manager. But the properties...
4
by: Joergen Bech | last post by:
Where, o where is my little "Solution Configurations" drop-down box? I then noticed that the Configuration Manager is absent from the Build menu. Dragging the item to the menu using "Customize"...
0
by: James Conrad StJohn Foreman | last post by:
Does IBM publish a schema for the database that the DB2 task manager uses? Although we've got the task manager client installed on some windows machines, (a) most of our desktop machines are Apple...
1
by: arunkumar_m2001 | last post by:
Hi, I need help. I transferred my database from one server to another by using attach detach process.I created all the jobs which existed in the old server in the new server. Everything looked...
5
by: NEWSGROUPS | last post by:
I have some users that continuously end task on my database when a search takes to long. In turn corrupting the application. Is there any way to prevent this? The Microsoft KB for Methods That Can...
0
by: narayan2586 | last post by:
Hi all, When ever i am trying to connect my application it's giving below error message: RemoteException occurred in server thread; nested exception is: java.rmi.RemoteException: ; nested...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.