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 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

Ask A Question

Stats

  • Questions 507k
  • Answers 507k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer this will give you the rows from the table where… May 16, 2026 at 3:55 pm
  • Editorial Team
    Editorial Team added an answer You can build or clauses dynamically from lists: clauses =… May 16, 2026 at 3:55 pm
  • Editorial Team
    Editorial Team added an answer This code should work: urlpatterns = patterns('', (r'^', include('preview_signup.urls')), )… May 16, 2026 at 3:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am working in LaTeX, and when I create a pdf file (using LaTeX
I made a toolbar with two buttons on it. I then created two outlets
Here is one very interesting problem. I am using SQL Server 2008. I have
In Java, how do I convert List<?> to List<T> using a general purpose method
my purpose is to display a UITableView with cells with a style like: TextA
I had a bunch of code in an activity that displays a running graph
I'm using castle windsor for a pet-project I'm working on. I'm starting to notice
What are some good desktop application projects I could code up that would be
I want to create a simple stacking window manager (in C ) for private
I have the following c++ code int factorial(int n){ if(n==0){ return 1; } return

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.