我只是在学习单选按钮,并且认为自从它编译好以后我终于得到了它。但是当我尝试运行程序时,会发生以下错误:
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)
我不知道我做错了什么,所以如果你能指出我正确的方向呢?或者向我解释为什么我在做什么是错的?
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: ");
rb1 = new JRadioButton(a, true);
rb1 = new JRadioButton(b);
rb1 = new JRadioButton(c);
rb1 = 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();
}
}
答案 0 :(得分:2)
您已将所有单选按钮命名为rb1
,因此其他组件永远不会被初始化
rb1 = new JRadioButton(a, true);
rb2 = new JRadioButton(b);
rb3 = new JRadioButton(c);
rb4 = new JRadioButton(d);
答案 1 :(得分:1)
不
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);
糟糕。
这里的关键不是你的错误,拍摄我们都犯了类似的错误。关键是要学习如何调试NPE。当你得到一个,检查抛出异常的行,找出哪个变量为null,然后回头尝试解决它。 那 是关键。
答案 2 :(得分:0)
变化
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();
}
}