Java 单选按钮赢得';不能正确显示
我只是在学习单选按钮,并认为我终于得到了它,因为它编译得很好。但当我尝试运行程序时,会出现以下错误:Java 单选按钮赢得';不能正确显示,java,radio-button,jpanel,Java,Radio Button,Jpanel,我只是在学习单选按钮,并认为我终于得到了它,因为它编译得很好。但当我尝试运行程序时,会出现以下错误: Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1090) at java.awt.Container.add(Container.java:410) at ActionFrame.makeComponents(ActionFrame.ja
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:410)
at ActionFrame.makeComponents(ActionFrame.java:43)
at ActionFrame.<init>(ActionFrame.java:20)
at ActionFrame.main(ActionFrame.java:80)
不是:
但是:
哎呀
这里的关键不是你的错误,我们都会犯类似的错误。关键是学习如何调试NPE。当您得到一个异常时,检查抛出异常的行,找出哪个变量为null,然后回过头来尝试解决它这是关键。您已经命名了所有单选按钮
rb1
,因此其他组件永远不会初始化
rb1 = new JRadioButton(a, true);
rb2 = new JRadioButton(b);
rb3 = new JRadioButton(c);
rb4 = new JRadioButton(d);
改变
进入
原始代码(带更改)
@用户2877117:不,你不是白痴;您只是还没有学会调试NPE。请阅读我的编辑。
rb1 = new JRadioButton(a, true);
rb1 = new JRadioButton(b);
rb1 = new JRadioButton(c);
rb1 = new JRadioButton(d);
rb1 = new JRadioButton(a, true);
rb2 = new JRadioButton(b);
rb3 = new JRadioButton(c);
rb4 = new JRadioButton(d);
rb1 = new JRadioButton(a, true);
rb2 = new JRadioButton(b);
rb3 = new JRadioButton(c);
rb4 = new JRadioButton(d);
rb1 = new JRadioButton(a, true);
rb1 = new JRadioButton(b);
rb1 = new JRadioButton(c);
rb1 = new JRadioButton(d);
rb1 = new JRadioButton(a, true);
rb2 = new JRadioButton(b);
rb3 = new JRadioButton(c);
rb4 = new JRadioButton(d);
public class ActionFrame extends JFrame {
JLabel messageLabel;
JRadioButton rb1;
JRadioButton rb2;
JRadioButton rb3;
JRadioButton rb4;
String a = "Football";
String b = "Basketball";
String c = "Baseball";
String d = "Hockey";
public ActionFrame() {
setTitle("Favorite Sports");
setSize (400,200);
setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
JPanel myStuff = makeComponents();
add(myStuff);
setVisible(true);
}
private JPanel makeComponents() {
JPanel myPanel = new JPanel();
messageLabel = new JLabel("Select your favorite sport: ");
/// changes are here...
rb1 = new JRadioButton(a, true);
rb2 = new JRadioButton(b);
rb3 = new JRadioButton(c);
rb4 = new JRadioButton(d);
ButtonGroup group = new ButtonGroup();
group.add(rb1);
group.add(rb2);
group.add(rb3);
group.add(rb4);
myPanel.add(rb1);
myPanel.add(rb2);
myPanel.add(rb3);
myPanel.add(rb4);
rb1.addActionListener( new BList() );
rb2.addActionListener( new BList() );
rb3.addActionListener( new BList() );
rb4.addActionListener( new BList() );
myPanel.add(messageLabel);
return myPanel;
}
private class BList implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == rb1){
System.out.println("Your favorite sport is " + a +".");
}
else if(e.getSource() == rb2){
System.out.println("Your favorite sport is " + b +".");
}
else if(e.getSource() == rb3){
System.out.println("Your favorite sport is " + c +".");
}
else if(e.getSource() == rb4){
System.out.println("Your favorite sport is " + d +".");
}
}
}
public static void main(String[] args) {
new ActionFrame();
}
}