Search code examples
javaswingnullpointerexceptionjapplet

Start: Applet Not Initialized error


I don't get any compiler errors but when I try to open the applet it gives me the Start: Applet Not Initialized error. Can someone tell me what is wrong with my code or give me some pointers? This is the what I get in the compiler, if it helps.

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();

    }

}

this is what my compiler shows

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)

Solution

  • You're trying to add null components to your 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!
    

    Create your components first before adding null items to your containers:

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

    More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me.