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 9190597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:29:56+00:00 2026-06-17T20:29:56+00:00

I am trying to show text – You pressed Button when you pressed one

  • 0

I am trying to show text – “You pressed Button” when you pressed one of buttons.
I am getting an NullPointerException. I have initialized the buttons inside the constructor of the class and after initialization, I called the following method from main().

Here is the code:

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

public class ButtonDemo implements ActionListener{
    JLabel jlab;

    ButtonDemo(){
        JFrame jfrm = new JFrame("A Button Example");

        jfrm.setLayout(new FlowLayout());

        jfrm.setSize(220, 90);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton jbtnUp = new JButton("Up");
        JButton jbtnDown = new JButton("Down");

        jbtnUp.addActionListener(this);
        jbtnDown.addActionListener(this);

        jfrm.add(jbtnUp);
        jfrm.add(jbtnDown);

        JLabel jlab = new JLabel("Press a button.");

        jfrm.add(jlab);
        jfrm.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getActionCommand().equals("Up"))
            jlab.setText("You pressed Up.");
        else
            jlab.setText("You pressed Down.");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ButtonDemo();
            }
        });
    }
}

What is the reason for this exception and how can I solve it?
Regards.

  • 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-17T20:29:58+00:00Added an answer on June 17, 2026 at 8:29 pm

    Your code is shadowing the variable jlab by re-declaring it in the constructor leaving the class field null. Don’t do that and your NPE will go away.

    i.e., change this:

    ButtonDemo(){
        JFrame jfrm = new JFrame("A Button Example");
    
        // ...
    
        // the variable below is being re-declared in the constructor and is thus
        // local to the constructor. It doesn't exist outside this block.
        JLabel jlab = new JLabel("Press a button.");
    
        // ...
    }
    

    to this:

    ButtonDemo(){
        JFrame jfrm = new JFrame("A Button Example");
    
        // ...
    
        jlab = new JLabel("Press a button."); // note the difference!
    
        // ...
    }
    

    A key to solving NPE’s is to carefully inspect the line that is throwing the exception as one variable being used on that line is null. If you know that, you can usually then inspect the rest of your code and find the problem and solve it.

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

Sidebar

Related Questions

I have a place where I am trying to show code in a text
I'm trying to split a button in half so that I can show text
I am trying to show and hide text on a site. But I have
I am trying to show my text class when I hover the img and
I am trying to show/hide multiple divs with the one button, but I am
I'm trying to show/hide some of text in a button. the button is <button
I am trying to show text in a console launched from the click button.
I am using TinyMCE Editor Extender and trying to show the text on clicking
I'm trying to show test run log (text file in a windows server) to
I'm trying to show a list of links (in index.html) to text files in

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.