Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem with CardLayout

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with CardLayout

    Hey guys I'm trying to make a GUI which can navigate through JTextAreas when a Next or Previous button is pushed. I'm using CardLayout to switch between the JTextArea. I can get the program to compile but every time I push the next or previous button I always get a NullPointerException. I don't know why the CardLayout object is null in the next and previous methods and its making me really confused. Any help is appreciated thx guys!

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;

    /**
    *The AsciiCanvas class creates the text area for the ascii animation
    */

    public class AsciiCanvas extends JTextArea
    {
    private JPanel jp1;
    private JPanel jp2;
    private JPanel jp3;
    private JPanel jp4;
    private JPanel jp5;
    private JPanel jp6;
    private JPanel jp7;
    private JPanel jp8;
    private JPanel jp9;
    private JPanel jp10;
    private JTextArea text1;
    private JTextArea text2;
    private JTextArea text3;
    private JTextArea text4;
    private JTextArea text5;
    private JTextArea text6;
    private JTextArea text7;
    private JTextArea text8;
    private JTextArea text9;
    private JTextArea text10;

    private JPanel cardPanel;
    private CardLayout cl;
    private int currentCard = 1;

    private JFileChooser fc;


    public AsciiCanvas()
    {
    Font mono = new Font("Monospaced", Font.PLAIN, 12);

    cardPanel = new JPanel();
    CardLayout cl = new CardLayout();
    cardPanel.setLayout(cl);


    JTextArea text1 = new JTextArea();
    JTextArea text2 = new JTextArea();
    JTextArea text3 = new JTextArea();
    JTextArea text4 = new JTextArea();
    JTextArea text5 = new JTextArea();
    JTextArea text6 = new JTextArea();
    JTextArea text7 = new JTextArea();
    JTextArea text8 = new JTextArea();
    JTextArea text9 = new JTextArea();
    JTextArea text10 = new JTextArea();



    JPanel jp1 = new JPanel();
    JPanel jp2 = new JPanel();
    JPanel jp3 = new JPanel();
    JPanel jp4 = new JPanel();
    JPanel jp5 = new JPanel();
    JPanel jp6 = new JPanel();
    JPanel jp7 = new JPanel();
    JPanel jp8 = new JPanel();
    JPanel jp9 = new JPanel();
    JPanel jp10 = new JPanel();

    jp1.add(text1);
    jp2.add(text2);
    jp3.add(text3);
    jp4.add(text4);
    jp5.add(text5);
    jp6.add(text6);
    jp7.add(text7);
    jp8.add(text8);
    jp9.add(text9);
    jp10.add(text10);

    cardPanel.add(jp1, "1");
    cardPanel.add(jp2, "2");
    cardPanel.add(jp3, "3");
    cardPanel.add(jp4, "4");
    cardPanel.add(jp5, "5");
    cardPanel.add(jp6, "6");
    cardPanel.add(jp7, "7");
    cardPanel.add(jp8, "8");
    cardPanel.add(jp9, "9");
    cardPanel.add(jp10, "10");

    setBorder(BorderFactory.createTitledBorder("Animat ion here"));
    setFont(mono);

    fc = new JFileChooser();
    }


    public void Next()
    {
    if(currentCard < 10)
    {

    currentCard +=1;
    cl.show(cardPanel, "" + (currentCard));
    }


    }

    public void Previous()
    {
    if(currentCard > 1)
    {
    currentCard -= 1;
    cl.show(cardPanel, "" + (currentCard));


    }
    }




    Error Message*******

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at AsciiCanvas.Next(AsciiCanvas.java:108)
    at AsciiArt$btnOneListener.actionPerformed(AsciiArt.j ava:72)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6504)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:626 9)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4860)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 86)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713 )
    at java.awt.Component.dispatchEvent(Component.java:46 86)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101 )
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with CardLayout

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at AsciiCanvas.Next(AsciiCanvas.java:108)
    There is a variable with a null value at line 108. Look at that line, find the null variable and backtrack in the code to see why that variable does not have a valid value.
    Use println to print out the variables' values to see which variable is null.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  2. CardLayout wont go to the next card
    By pottsiex5 in forum AWT / Java Swing
    Replies: 2
    Last Post: November 25th, 2011, 04:04 PM
  3. How to use CardLAyout without using applets???
    By divyarattan in forum AWT / Java Swing
    Replies: 4
    Last Post: August 9th, 2011, 11:19 AM
  4. Cardlayout Question
    By wagb278 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 10th, 2011, 03:23 PM
  5. Cardlayout help
    By phantomswordsmen in forum AWT / Java Swing
    Replies: 1
    Last Post: December 22nd, 2010, 02:36 PM