开始:Applet未初始化错误

时间:2015-03-18 01:11:34

标签: java swing nullpointerexception japplet

我没有遇到任何编译器错误,但是当我尝试打开applet时,它会给我Start: Applet Not Initialized错误。有人能告诉我我的代码有什么问题或者给我一些指示吗?如果它有帮助的话,这就是我在编译器中得到的结果。

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
//import libraries

public class caffine extends JPanel implements ActionListener{//class header - implement listeners (actionlistener (for button))

    private boolean macchiato=false;//declare global booleans
    private boolean mocha=false;//declare global button
    private boolean americano=false;
    private boolean espresso=false;
    private JTextField mchto=new JTextField("Macchiato");;
    private JTextField mcha=new JTextField("Mocha");;
    private JTextField acano=new JTextField("Americano");
    private JTextField espo=new JTextField("Espresso");
    private JButton mchtob;
    private JButton mchab;
    private JButton acanob;
    private JButton espob;
    private String b1;
    private String b2;
    private String b3;
    private String b4;
    private int i1;
    private int i2;
    private int i3;
    private int i4;
    private int total;

    public caffine(){//constructor
        add(mchto);//add button to panel
        add(mchtob);

        add(mcha);
        add(mchab);

        add(acano);
        add(acanob);

        add(espo);
        add(espob);
        //add the buttons
        //add button to panel

        mchtob.addActionListener(this);//add listener to button
        mchab.addActionListener(this);
        acanob.addActionListener(this);
        espob.addActionListener(this);

}
    public void paintComponent(Graphics g){//paint component
        g.drawString("How many 16 fl oz cups of each coffee do you drink per week?",0,10);//how do you like your coffee????
        g.drawString("You ingest approximately " +total+ " mg of caffine per week.  To have a good night's sleep, you should ingest less than 500 mg of caffine per week.",0,20);//add all the mg of caffine at bottom
        //print your total caffine intake of the week, assuming 2x tall per week  @ starbucks


}

    public void actionPerformed(ActionEvent e){//event handler: actionPerformed (for button)
        b1=mchto.getSelectedText();
        b2=mcha.getSelectedText();
        b3=acano.getSelectedText();
        b4=espo.getSelectedText();
         i1 = Integer.parseInt( b1 );
         i2 = Integer.parseInt( b2);
         i3 = Integer.parseInt( b3 );
         i4 = Integer.parseInt( b4);
         total=i1+i2+i3+i4;
         total=60*total;

        repaint();

    }

}

这是我的编译器显示的内容

java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1095)
    at java.awt.Container.add(Container.java:415)
    at caffine.<init>(caffine.java:39)
    at caffineapplet.init(caffineapplet.java:5)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:2)

您正在尝试向GUI添加空组件:

public class caffine extends JPanel implements ActionListener{//class header - implement listeners (actionlistener (for button))

    private boolean macchiato=false;//declare global booleans
    // ....
    private JButton mchtob;  // this guy is null!
    // ...

    public caffine(){//constructor
        add(mchto);
        add(mchtob);  // he's null still!

在向容器中添加空项目之前,首先创建组件:

private JButton mchtob = new JButton(/* put your action here */);

更重要的是,您需要学习如何调试NPE(NullPointerException)的一般概念。 您应该批判性地读取异常的堆栈跟踪以查找出错的代码行,抛出异常的行,然后仔细检查该行,找出哪个变量为null,然后追溯到您的代码,看看为什么。你会一次又一次地碰到这些,相信我。

答案 1 :(得分:0)

许多组件未初始化,因此当您调用它时会抛出NPE。