开始:小程序未初始化错误

Start: Applet Not Initialized error

我没有收到任何编译器错误,但是当我尝试打开该小程序时,出现 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)

您正在尝试向 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) 的一般概念。 你应该批判性地阅读异常的堆栈跟踪,找到出错的代码行,抛出异常的行,然后仔细检查那行,找出哪个变量为空,然后跟踪回到你的代码看看为什么。相信我,你会 运行 一次又一次地进入这些。

您的许多组件都没有初始化,所以当您调用它时它会抛出 NPE。