-2

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)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
odeng
  • 49
  • 1
  • 9
  • 1
    That's not a *compiler* message, but rather an exception thrown by the JVM. If you've done even a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you. So what's happening on that line of code? Which line of code is the message complaining about: `caffine.java:39`? – Hovercraft Full Of Eels Mar 18 '15 at 01:15
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Mar 18 '15 at 01:19
  • Note that: `public void paintComponent(Graphics g){//paint component` should be `public void paintComponent(Graphics g){//paint component super.paintComponent(g); // paint parent first!` – Andrew Thompson Mar 18 '15 at 01:37
  • Thanks so much, it works now! Sorry if my questions were really bad because I just started learning java this year. – odeng Mar 18 '15 at 01:42

2 Answers2

2

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Wait the applet still doesn't initialize for some reason?? Also thanks for the advice on stack traces but I have never really learned them. – odeng Mar 18 '15 at 01:35
  • `"...for some reason"` helps little. You're going to have to dig for the details and inform us. – Hovercraft Full Of Eels Mar 18 '15 at 01:36
  • yeah sorry this is what the compiler says java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1095) at java.awt.Container.add(Container.java:415) at caffine.(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) – odeng Mar 18 '15 at 01:38
  • 1
    @odeng: you've got another null pointer exception. Come on, you should know how to fix these now. – Hovercraft Full Of Eels Mar 18 '15 at 01:39
  • 1
    *"yeah sorry this is what the compiler says java.lang.NullPointerException at"* As mentioned before: 1) It's not the compiler reporting those messages. Perhaps it is your IDE, but it is the run-time (the JRE) that actually generates them. 2) The way to debug them. Are you expecting us to spoon feed the answer to every single fix? – Andrew Thompson Mar 18 '15 at 01:40
0

Many of your components aren't initialized, so when you call it it throws a NPE.

Illorum
  • 123
  • 10