• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Radio button class returns null pointer exception

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic