import java.applet.*;
import java.awt.*;
import java.awt.event.*;class Mypanel extends Panel implements ActionListener {
Button button1,button2,button3;
Color backColor;
Mypanel() {
button1 = new Button("确定");
button1 = new Button("取消");
button1 = new Button("保存");
add(button1);
add(button2);
add(button3);
setBackground(Color.pink);
backColor = getBackground();
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1) {
setBackground(Color.yellow);
}
else if(e.getSource()==button2) {
setBackground(backColor);
}
}
}public class Example11_1 extends Applet {
Mypanel panel1,panel2,panel3;
Button button;
public void init() {
panel1 = new Mypanel();
panel2 = new Mypanel();
panel3 = new Mypanel();
button = new Button("我不在那些面板里面");
add(panel1);
add(panel2);
add(panel3);
add(button);
}
}
d:\我的文档\桌面\ceshi>javac Example11_1.javad:\我的文档\桌面\ceshi>appletviewer Example11_1.htm
java.lang.NullPointerException
        at java.awt.Container.addImpl(Container.java:1014)
        at java.awt.Container.add(Container.java:348)
        at Mypanel.<init>(Example11_1.java:13)
        at Example11_1.init(Example11_1.java:34)
        at sun.applet.AppletPanel.run(AppletPanel.java:424)
        at java.lang.Thread.run(Thread.java:619)不知什么原因?

解决方案 »

  1.   

    button1 = new Button("确定");
    button1 = new Button("取消");
    button1 = new Button("保存");怎么都是button1啊
      

  2.   

          button1 = new Button("确定");
            button1 = new Button("取消");
            button1 = new Button("保存");
            add(button1);
            add(button2);
            add(button3);
    你的button2 button3 没有NEW 
      

  3.   

    你 button2 没有初始化
    应该是
             button1 = new Button("确定");
            button2 = new Button("取消");
            button3 = new Button("保存"); 

      

  4.   

    button1 = new Button("确定");
    button1 = new Button("取消");
    button1 = new Button("保存");
    改成
    button1 = new Button("确定");
    button2 = new Button("取消");
    button3 = new Button("保存");