jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes Radio button class returns null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Radio button class returns null pointer exception" Watch "Radio button class returns null pointer exception" New topic

Radio button class returns null pointer exception

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53

I'm attempting to put together a RadioGUI class that takes a string array as input and returns the value selected from a group of radio buttons. In order to capture the actionlistener result, I'm storing the values in a Bean class. The trouble is, I can't seem to get around a null pointer exception when I call the RadioGUI class. Any thoughts on this would be welcome. Please see below for the Main, Bean and RadioGUI classes and the stack trace. Thanks in advance.

// ********************** Main class ************************
public class Main {

// public static RadioGUI radioGUI;

* @param args the command line arguments
public static void main(String[] args) {
Bean bean = new Bean();
String choices[] = {"A", "B", "C"};
bean = RadioGUI.getSelection(bean);
String selection = bean.getSelection();

public static void showMessageOnTop(String s) {
JFrame f = new JFrame("whizzo");
f.setSize(400, 150);
JOptionPane op = new JOptionPane();
op.setRootFrame(f); // Makes op always on top like f
op.showMessageDialog(f, s);
System.out.println("Last line of showMessageOnTop");

// ************************ Bean class ******************************
package test1;

public class Bean {

private String[] choices;
private String selection;

public void setChoices(String[] choices) {
this.choices = choices;

public String[] getChoices() {
return choices;

public void setSelection(String selection) {
this.selection = selection;

public String getSelection() {
return selection;

// ************************** RadioGUI class ******************************

package test1;

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

public class RadioGUI extends JPanel {

public static JLabel title = new JLabel("Select object to modify:", JLabel.CENTER);
public static JFrame frame;
public static String simpleDialogDesc = "BIM objects";
public static String[] choices;
private Bean bean;

public RadioGUI() {
JPanel choicePanel = createSimpleDialogBox();
choicePanel.setBorder(BorderFactory.createEmptyBorder(20, 60, 10, 60));
setLayout(new BorderLayout());
add(title, BorderLayout.NORTH);
add(choicePanel, BorderLayout.CENTER);

private JPanel createSimpleDialogBox() {
choices = bean.getChoices();
final int numButtons = choices.length;
JRadioButton[] radioButtons = new JRadioButton[numButtons];
final ButtonGroup group = new ButtonGroup();
JButton submitButton = null;

for (int i = 0; i < numButtons; i++) {
radioButtons[i] = new JRadioButton("<html>" + choices[i] + "</html>");

submitButton = new JButton("Submit");
submitButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
String selection = group.getSelection().getActionCommand();
return createPane(simpleDialogDesc + ":", radioButtons, submitButton);

private static JPanel createPane(String description, JRadioButton[] radioButtons, JButton showButton) {
int numChoices = radioButtons.length;
JPanel box = new JPanel();
JLabel label = new JLabel(description);

box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));

for (int i = 0; i < numChoices; i++) {

JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(box, BorderLayout.NORTH);
pane.add(showButton, BorderLayout.SOUTH);
System.out.println("returning pane");
return pane;

public static Bean getSelection(Bean bean) {
frame = new JFrame("BIM selection");
RadioGUI radioGUI = new RadioGUI();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1, 1));
contentPane.add(new RadioGUI());
return radioGUI.getBean();

public Bean getBean() {
return bean;

public void setBean(Bean bean) {
this.bean = bean;

// *********************** Stack trace ******************************

Exception in thread "main" java.lang.NullPointerException
at test1.RadioGUI.createSimpleDialogBox(RadioGUI.java:24)
at test1.RadioGUI.<init>(RadioGUI.java:16)
at test1.RadioGUI.getSelection(RadioGUI.java:73)
at test1.Main.main(Main.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

The stack trace says your error is in this method:

Specifically, it's at line 24, wherever that may be. My guess is that it's one of the first two lines, but you should ask your text editor which line is line 24. When you find that line, your job is to determine why you are trying to call a method of a variable which is null.

By the way I used the code tags when I reposted that code fragment. Notice how much easier it makes the code to read? The easiest way to do that is to paste your code into the text box, then select all of it, then click on the "Code" button which you'll see above the text box.
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Thanks for the reply Paul. Also, thanks for the tip about code tags. I will use them going forward.The line in question is line 24 in method createSimpleDialogBox of the RadioGUI class. This populates a string array, named choices, with values retrieved from the Bean class. choices is defined as public and static in RadioGUI. choices is the array which is throwing a null pointer exception.

It appears that RadioGUI cannot be instantiated unless choices is already defined.

The bean is populated in the Main class and passed to the getSelection method of RadioGUI. getSelection creates a new radioGUI instance and passes the bean to it:

Bean contains standard setter and getter methods:

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
The null pointer error was a very simple fix but it doesn't address the underlying issues. I'm trying to write a standalone radio button dialog class that inputs a string array of choices and returns the selection. The trouble is, the radio button dialog depends on an action listener. When I call the RadioGUI class from Main, the selection is always null because Main doesn't wait for the actionlistener results. I'll continue this in another thread.

BTW: The fix to the null pointer is below. I just retrieved choices from the bean before I instantiated RadioGUI. Duh.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Jerry Goldsmith wrote:The null pointer error was a very simple fix but it doesn't address the underlying issues. I'm trying to write a standalone radio button dialog class that inputs a string array of choices and returns the selection.

Then why are you using a JFrame? If you want a dialog, use a JDialog. Follow the link (JDialog) to the API documentation. Near the beginning of the docs you'll find a link to the tutorial How to Make Dialogs. I strongly recommend you read it and use its examples as a base for your own code.
I agree. Here's the link: http://aspose.com/file-tools
subject: Radio button class returns null pointer exception
It's not a secret anymore!