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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:07:22+00:00 2026-05-14T01:07:22+00:00

I have a static method which sets a variable: static String[] playersNames; public static

  • 0

I have a static method which sets a variable:

static String[] playersNames;
public static void setParameters(String[] players) {
playersNames = players;
}

Then I have a static block:

static {
    JRadioButton option;
    ButtonGroup group = new ButtonGroup();
    // Wright a short explanation of what the user should do.
    partnerSelectionPanel.add(new JLabel("Pleas select a partner:"));
    // Generate radio-buttons corresponding to the options available to the player.
            // Bellow is the problematic line causing the null pointer exception:
    for (String playerName: playersNames) {         
        final String pn = playerName;
        option = new JRadioButton(playerName, false);
        option.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent evt) {
                partner = pn;
            }
        });
        partnerSelectionPanel.add(option);
        group.add(option);
    }
    partnerSelectionPanel.add(label);
    // Add the "Submit" button to the end of the "form".
    JButton submitButton = new JButton("Submit");
    submitButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent evt) {
            partnerSelected();
        }
    });
    partnerSelectionPanel.add(submitButton);
}

Compiler does not complain about anything but when I try to execute the code I get problems. In this place SelectPartnerGUI.setParameters(players); I have:

Exception in thread “main”
java.lang.ExceptionInitializerError.

and it is cause by java.lang.NullpointerException at this place for (String playerName: playersNames).

Does my program do not see the palyersNames?


The first time I refer to the class in this way: SelectPartnerGUI.setParameters(players);. And in my class I have the setParameters method before the problematic static block. So, why this static block is called before the setParameters method is called?

  • 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-14T01:07:22+00:00Added an answer on May 14, 2026 at 1:07 am

    Any static initializer blocks get executed as soon as the class get loaded. You can impossibly call a method on a class before the static initializer runs. You’ll need to set the playersNames in the static initializer block itself. Keep in mind that they’re executed in the order as they appear in the code. A better approach IMO is to rewrite the whole thing and make use of a constructor to construct a class.

    Update: as per your edit:

    The first time I refer to the class in this way: SelectPartnerGUI.setParameters(players);. And in my class I have the setParameters method before the problematic static block. So, why this static block is called before the setParameters method is called?

    The static block is called as soon as the class get loaded. This already happens when the JVM encounters SelectPartnerGUI for the first time. You cannot call any (static) methods on a class before it’s loaded by JVM. It’s like that you cannot drive the car before you turn on the engine .

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There will be the issue of publication (meaning you will… May 14, 2026 at 7:21 am
  • Editorial Team
    Editorial Team added an answer This can be an extensive topic. First of all: What… May 14, 2026 at 7:21 am
  • Editorial Team
    Editorial Team added an answer Try the following code: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">… May 14, 2026 at 7:21 am

Related Questions

In essence, I want to put a variable on the stack, that will be
Is there anyway of having a base class use a derived class's static variable
I have a custom built application framework written in PHP which I have been
I'm working on creating a call back function for an ASP.NET cache item removal
I have created a new solution with a minimal amount of code that represents

Trending Tags

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

Top Members

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.