请各位大侠帮忙看看以下的代码,初次学习java,以下代码运行时报如下错误,不知是何故,该如何修改,请各位大侠执教一二,谢谢了!!!报错信息如下:
java.lang.NullPointerException
at Panel1.<init>(Exercise6_2.java:11)
at MyWindow.<init>(Exercise6_2.java:26)
at Exercise6_2.<init>(Exercise6_2.java:51)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
代码:import java.applet.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;
class Panel1 extends JPanel{
JRadioButton box1,box2,box3,box4;
ButtonGroup g;
Panel1(){
setLayout(new GridLayout(1,4));
box1=new JRadioButton("足球");
box2=new JRadioButton("篮球");
box3=new JRadioButton("排球");
box4=new JRadioButton("桌球");
g.add(box1);g.add(box2);g.add(box3);g.add(box4);
add(box1);add(box2);add(box3);add(box4);
}
}
class MyWindow extends JFrame implements ItemListener{
Panel1 pane1;
JLabel label;
JTextField tf;
MyWindow(String s){
super(s);
Container con=this.getContentPane();
con.setBackground(Color.cyan);
con.setLayout(new GridLayout(2,2));
this.setLocation(100,100);
this.setSize(400,100);
pane1=new Panel1();
label=new JLabel("你选择的运动是:",JLabel.CENTER);
tf=new JTextField(20);
con.add(pane1);
con.add(label);
con.add(tf);
pane1.box1.addItemListener(this);
pane1.box2.addItemListener(this);
pane1.box3.addItemListener(this);
pane1.box4.addItemListener(this);
this.setVisible(true);
this.pack();
}
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable()==pane1.box1)
tf.setText(pane1.box1.getName());
else if(e.getItemSelectable()==pane1.box2)
tf.setText(pane1.box2.getName());
else if(e.getItemSelectable()==pane1.box3)
tf.setText(pane1.box3.getName());
else
tf.setText(pane1.box4.getName());
}
}
public class Exercise6_2 extends Applet {
MyWindow myWin=new MyWindow("窗口");
}

解决方案 »

  1.   

    下边是简单的该法,暂时让程序运行起来。源代码中部分变量的定义 不太合乎习惯。import java.applet.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;class Panel1 extends JPanel {
    JRadioButton box1, box2, box3, box4;
    ButtonGroup g = new ButtonGroup();  //<-此处
    Panel1() {
    setLayout(new GridLayout(1, 4));
    box1 = new JRadioButton("足球");
    box2 = new JRadioButton("篮球");
    box3 = new JRadioButton("排球");
    box4 = new JRadioButton("桌球");
    g.add(box1);
    g.add(box2);
    g.add(box3);
    g.add(box4);
    add(box1);
    add(box2);
    add(box3);
    add(box4);
    }
    }class MyWindow extends JFrame implements ItemListener {
    Panel1 pane1;
    JLabel label;
    JTextField tf; MyWindow(String s) {
    super(s);
    Container con = this.getContentPane();
    con.setBackground(Color.cyan);
    con.setLayout(new GridLayout(2, 2));
    this.setLocation(100, 100);
    this.setSize(400, 100);
    pane1 = new Panel1();
    label = new JLabel("你选择的运动是:", JLabel.CENTER);
    tf = new JTextField(20);
    con.add(pane1);
    con.add(label);
    con.add(tf);
    pane1.box1.addItemListener(this);
    pane1.box2.addItemListener(this);
    pane1.box3.addItemListener(this);
    pane1.box4.addItemListener(this);
    this.setVisible(true);
    this.pack();
    } public void itemStateChanged(ItemEvent e) {
    if (e.getItemSelectable() == pane1.box1)
    tf.setText(pane1.box1.getName());
    else if (e.getItemSelectable() == pane1.box2)
    tf.setText(pane1.box2.getName());
    else if (e.getItemSelectable() == pane1.box3)
    tf.setText(pane1.box3.getName());
    else
    tf.setText(pane1.box4.getName());
    }
    }public class Test extends Applet {
    MyWindow myWin = new MyWindow("窗口");
    }