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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:50:23+00:00 2026-05-15T17:50:23+00:00

Purpose of the code : Create two Buttons(button1 and button2). When User clicks button1,

  • 0

Purpose of the code : Create two Buttons(button1 and button2). When User clicks button1, change the text of button2. When User clicks button2, change the text of button1.

Here’s the code I’m using :

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

public class multiAL {
JButton button1;
JButton button2;
JFrame frame;
public static void main(String[] args) {
    multiAL setterAL = new multiAL();
    setterAL.go();
}

public void go() {
    button1 =  new JButton("Click me, I'm One");
    button2 =  new JButton("Click me, I'm Two");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(BorderLayout.WEST, button1);
    frame.getContentPane().add(BorderLayout.EAST, button2);
    frame.setVisible(true);
    button1.addActionListener(new b1L());
    button2.addActionListener(new b2L());
}

class b1L implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        button2.setText("What??, you clicked 1??");
    }
}

class b2L implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        button1.setText("What??, you clicked 2??");
    }
  }
}

It compiles perfectly, but when I run it I receive following error :
Exception in thread "main" java.lang.NullPointerException
at multiAL.go(multiAL.java:17)
at multiAL.main(multiAL.java:11)

Till now, I’ve encountered only compile-time errors. So there are two question which I want to ask:

1) What’s wrong with the code?
2) How to track down runtime errors?

  • 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-15T17:50:24+00:00Added an answer on May 15, 2026 at 5:50 pm

    frame is not initialized, so it resolves to null and you can’t call methods on null objects. Like you initialized button1 and button2 you should also initialize frame.

    frame = new JFrame();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question, What have I done wrong here. The purpose of this code is
Purpose: Create a program that takes two separate files, opens and reads them, assigns
I've got following (simplified for example purpose) code and it works: void log(const string
The purpose of this code is to pull upgrade.zip from a central server, extract
Suppose i have the code snippet as follows : ( clarification purpose/not well formed
What is the purpose of these java.lang.annotation imports in this code? Why are they
One recommends me the following code apparently only in .zshrc without explaining its purpose
What is the main purpose of overloading operators in C++? In the code below,
I run the following code on a Windows platform. The purpose is the know
I am not sure about the exact purpose of the following Rampion's code .

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.