Why I do not see a static variable in a loop?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-22T12:08:30Z Indexed on 2010/03/22 12:11 UTC
Read the original article Hit count: 330

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about static