Greetings,
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.setChoices(choices);
bean = RadioGUI.getSelection(bean);
String selection = bean.getSelection();
showMessageOnTop(selection);
}
public static void showMessageOnTop(String s) {
JFrame f = new JFrame("whizzo");
f.setVisible(true);
f.setAlwaysOnTop(true);
f.setSize(400, 150);
JOptionPane op = new JOptionPane();
op.setRootFrame(f); // Makes op always on top like f
op.showMessageDialog(f, s);
f.dispose();
f.setVisible(false);
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>");
radioButtons[i].setActionCommand(choices[i]);
group.add(radioButtons[i]);
}
radioButtons[0].setSelected(true);
submitButton = new JButton("Submit");
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selection = group.getSelection().getActionCommand();
bean.setSelection(selection);
frame.dispose();
frame.setVisible(false);
return;
}
});
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));
box.add(label);
for (int i = 0; i < numChoices; i++) {
box.add(radioButtons[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();
radioGUI.setBean(bean);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1, 1));
contentPane.add(new RadioGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
return radioGUI.getBean();
}
public Bean getBean() {
return bean;
}
public void setBean(Bean bean) {
this.bean = bean;
}
}
// *********************** Stack trace ******************************
run:
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)