Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7712569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:39:53+00:00 2026-06-01T01:39:53+00:00

Hey guys I’m trying to make a GUI which can navigate through JTextAreas when

  • 0

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.

The error is coming from this piece of code

cl.show(cardPanel, "" + (currentCard));

Any help is appreciated thx guys!

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


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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:39:54+00:00Added an answer on June 1, 2026 at 1:39 am

    The problem is that cl is null, and here’s why:

    private CardLayout cl;
    
    ...
    
    public AsciiCanvas()
    {
        ...
        CardLayout cl = new CardLayout();
    

    In that constructor you’ve declared a new local variable, rather than assigning a value to the instance variable. The local variable shadows or hides the instance variable. Your consructor code should be:

    public AsciiCanvas()
    {
        ...
        cl = new CardLayout();
    

    (As an aside, I’d strongly recommend using String.valueOf(...) instead of "" + ... to convert a value to a string in a null-safe way. You’re not really interested in string concatenation, are you?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I can't seem to get the syntax here right, I'm just trying
Hey guys im trying to get a simple button masher up, What i want
Hey guys im trying to make it so when someone zooms in on my
Hey guys i am trying to automate my system at work for sending artwork
hey guys how to make these php html links codes possible. i have tried
Hey guys I'm trying to add the string 'url( ) ' around my a
Hey guys, I'm trying to get my head around LINQ and FP, so forgive
Hey guys i wrote a quick test. I want delete to call deleteMe which
Hey guys! I have this little problem: I have one ViewController which adds 2
Hey guys, I have a problem (again). This time I am trying to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.