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

  • SEARCH
  • Home
  • 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 6207573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:39:17+00:00 2026-05-24T05:39:17+00:00

I have two classes ( Main_Menu and CancelListener ). When i called an instance

  • 0

I have two classes (Main_Menu and CancelListener). When i called an instance of my CancelListener to use the proper actionPerformed method, i get a NullPointerException.

In my MainMenu.java class, i declare the button like this:

JButton button = new JButton();
button.addActionListener(new CancelListener());

Here is my CancelListener() class.

class CancelListener implements ActionListener {

    private Main_Menu menu;
    public JPanel mpan;

    public CancelListener() {
    }

    @Override
    public void actionPerformed(ActionEvent ae) {


        System.out.println("worked");
        try {

            CardLayout c1 = (CardLayout) (menu.MainPanel.getLayout());
            c1.show(menu.MainPanel, "AppPanel");
        } catch (Exception e) {

            System.out.println("Exception here " + e);
        }
    }
}

Further Information: MainPanel is a JPanel with a CardLayout structure. On the Cancel Button, i want to show a particular Panel. I wanted to separate my button view class from the button logic? it works when i dont use a separate class.

  • 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-05-24T05:39:18+00:00Added an answer on May 24, 2026 at 5:39 am

    You need the menu variable to be set:

    In CancelListener class:

    private Main_Menu menu;
    
    public CancelListener(Main_Menu menu) {
        this.menu = menu;
    }
    

    Then, when constructing:

    button.addActionListener(new CancelListener(this));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
I have two classes that each need an instance of each other to function.
I have two classes: Action class, that has a method for executing VBScript files,
Basically, I have two classes : - MyActivity.java - OtherClass.java Overview of MyActivity.java :
I am a beginner learning Java. I have two classes, public class myClassA {
Firstly, I am new to both android and Java. I have two classes, my
I have two classes with one extending the other. They both have a method
I have two classes gameWindow and gameEngine . The Main method is in gameWindow
I have two classes which extend Activity and need to call another class method
I have two classes with nested generics. Is there a way to get rid

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.