CardLayout Manager is giving some trouble

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • p vs np
    New Member
    • Mar 2009
    • 26

    CardLayout Manager is giving some trouble

    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 createAndShowGU I( ) 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 addComponentToP ane() in each of the classes Page0 a Page1, which return the JPanel of that particular page. This method is invoked in createAndShowGU I( ),

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

    Code:
       public void actionPerformed(ActionEvent e)
        {
            String next = " Next ";
            String back = " Back ";
            
            CardLayoutSelf c = new CardLayoutSelf();
            CardLayout cl = (CardLayout)(c.cards.getLayout());
            
            if(next.equalsIgnoreCase(e.getActionCommand()) )
            {
             cl.show(c.cards, "1");
            }
       }
    My createAndShowGU I() definition is as follows:

    Code:
    void createAndShowGUI()
        {
            cards = new JPanel(new CardLayout());
    
            JFrame frame = new JFrame(" CardLayouts");
            frame.setLocation(400,300);
            frame.setSize(600,450); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            Page0 p0 = new Page0();
            cards.add("0", p0.addComponentToPane());
    
            Page1 p1 = new Page1();
            cards.add("1", p1.addComponentToPane());
    
            frame.getContentPane().add (cards,BorderLayout.CENTER);
    
            //frame.pack();
            frame.setVisible(true);
    
        }
    The error I get, on clicking the "Next" button on Page 0 is :

    Exception in thread "AWT-EventQueue-0" java.lang.NullP ointerException
    at Page0.actionPer formed(CardLayo utSelf.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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • p vs np
      New Member
      • Mar 2009
      • 26

      #3
      Yes.
      I do realise the significance of the line number, and in my code, it corresponds to the statement:

      Code:
      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!?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        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.getLayo ut().

        Comment

        • p vs np
          New Member
          • Mar 2009
          • 26

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

          Thanks for the help :)

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

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

            Comment

            Working...