Connecting Tech Pros Worldwide Forums | Help | Site Map

CardLayout Manager is giving some trouble

Newbie
 
Join Date: Mar 2009
Location: India
Posts: 14
#1: Jun 26 '09
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.

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jun 26 '09

re: CardLayout Manager is giving some trouble


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.
Newbie
 
Join Date: Mar 2009
Location: India
Posts: 14
#3: Jun 26 '09

re: CardLayout Manager is giving some trouble


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!?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jun 26 '09

re: CardLayout Manager is giving some trouble


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().
Newbie
 
Join Date: Mar 2009
Location: India
Posts: 14
#5: Jun 26 '09

re: CardLayout Manager is giving some trouble


Silly me.
I had not made cards , the variable in class CardLayoutSelf static.

Thanks for the help :)
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: Jun 26 '09

re: CardLayout Manager is giving some trouble


I hope you are using How to use CardLayout as a reference while you are doing this.
Reply