0

Wrote my first java applet for a coding class I'm taking. keeps saying "Start: Applet not Intilized" I realize there is probably more issues in here, but I can't find them when the applet won't run. can anyone find my error?

import java.applet.*;  
import java.awt.*;   
import java.awt.event.*;   
import java.awt.image.BufferedImage;   
import java.text.DecimalFormat;   

import javax.swing.JOptionPane;   



public class danielDrinkMachine extends Applet   
{

    private Panel mainPanel;
    private Panel buttons1;
    private Panel buttons2;
    private Panel screen;

    private BufferedImage logo;

    private TextField money;

    private Label screenText;
    private Label titleText;

    private Button cola;
    private Button lemonLimeSoda;
    private Button rootBeer;
    private Button grapeSoda;
    private Button bottledWater;
    private Button formattingButton;

    private double currentMoney;

    private int colaCount = 20;
    private int llCount = 20;
    private int rbCount = 20;
    private int grapeCount = 20;
    private int waterCount = 20;


    private String format ="$#,##0.00";
    private DecimalFormat outFormat =new DecimalFormat(format);

    private final int width = 500;
    private final int height = 500;




    public void init()
    {
        screenText =new Label();

        setSize(WIDTH, HEIGHT);

        buildPanel();   

        add(mainPanel);

        setVisible(true);
    }

    private void buildPanel()
    {

        Label titleText = new Label("ALL DAY's VENDING MACHINES");

        money =new TextField();
        money.setEditable(true);

        Label screenText = new Label("Welcome! Drinks cost $0.75 each!");





        cola =new Button("Cola");
        lemonLimeSoda =new Button("Lemon Lime");
        grapeSoda =new Button("Grape Soda");
        rootBeer =new Button("Root Beer");
        bottledWater =new Button("Water");
        formattingButton =new Button("(Sold Out)");

        cola.addActionListener(new iAmTheListener());
        lemonLimeSoda.addActionListener(new iAmTheListener());
        grapeSoda.addActionListener(new iAmTheListener());
        rootBeer.addActionListener(new iAmTheListener());
        bottledWater.addActionListener(new iAmTheListener());
        formattingButton.addActionListener(new iAmTheListener());


        mainPanel.add(titleText, BorderLayout.PAGE_START);
        mainPanel.add(buttons1, BorderLayout.LINE_START);
        mainPanel.add(buttons2, BorderLayout.LINE_END);
        mainPanel.add(screen, BorderLayout.LINE_END);

        screen.setLayout(new GridLayout(1,2));
        screen.add(screenText);
        screen.add(money);

        buttons1.setLayout(new GridLayout(1,3));
        buttons1.add(cola);
        buttons1.add(lemonLimeSoda);
        buttons1.add(grapeSoda);

        buttons2.setLayout(new GridLayout(1,3));
        buttons2.add(rootBeer);
        buttons2.add(bottledWater);
        buttons2.add(formattingButton);




    }

    private class iAmTheListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent buttonPress)
        {
            String moMoney=money.getText();
            currentMoney =Double.parseDouble(moMoney); 

            if(currentMoney < 0.75)
            {
                int count = 0;

                if(count == 0)
                {
                    JOptionPane.showMessageDialog(null,"Drinks are $0.75 each. Enter more money");  
                    count++;
                }

                if(count == 1)
                {
                    JOptionPane.showMessageDialog(null,"Seriously, its not even real money.");
                    count++;
                }

                if(count == 2)
                {
                    JOptionPane.showMessageDialog(null,"Is this a joke? Its not funny.");
                    count++;
                }

                if(count == 3)
                {
                    JOptionPane.showMessageDialog(null,"Just type put a '1' in the money blank, please.");
                    count ++;
                }

                if(count >= 4)
                {
                    JOptionPane.showMessageDialog(null,"Haha! It's always funnier the " + count + "th time!");
                    count++;
                }

            }
            else if(currentMoney >= 0.75)
            { 
                if(buttonPress.getSource() == cola)
                { 
                    if(colaCount == 0)
                        screenText.setText("Sold out");
                    else if(colaCount != 0)
                    {
                        currentMoney -= 0.75;                               
                        screenText.setText(outFormat.format(currentMoney));
                        currentMoney = 0.0;
                        colaCount--;    
                    }
                    else;
                }
                else if(buttonPress.getSource() ==lemonLimeSoda)
                {
                    if(llCount ==0)
                    {
                        screenText.setText("Sold out");
                    }
                    else if(llCount !=0)
                    {
                        currentMoney -= 0.75;                           
                        screenText.setText(outFormat.format(currentMoney));
                        currentMoney =0.0;
                        llCount--;
                    }
                    else;
                }
                else if(buttonPress.getSource() ==grapeSoda)
                {
                    if(grapeCount ==0)
                    {
                        screenText.setText("Sold out");
                    }
                    else if(grapeCount !=0)
                    {
                        currentMoney -= 0.75;                           
                        screenText.setText(outFormat.format(currentMoney));
                        currentMoney =0.0;
                        grapeCount--;
                    }
                    else;
                }
                else if(buttonPress.getSource() ==rootBeer)
                {
                    if(rbCount ==0)
                    {
                        screenText.setText("Sold out of Root Beer");
                    }
                    else if(rbCount !=0)
                    {
                        currentMoney -= 0.75;                           
                        screenText.setText(outFormat.format(currentMoney));
                        currentMoney =0.0;
                        rbCount--;
                    }
                    else;
                }
                else if(buttonPress.getSource() ==bottledWater)
                {   
                    if(waterCount ==0)
                    {
                        screenText.setText("Sold out");
                    }
                    else if(waterCount !=0)
                    {
                        currentMoney -= 0.75;                           
                        screenText.setText(outFormat.format(currentMoney));
                        currentMoney =0.0;
                        waterCount--;
                    }
                    else;
                }
                else;

            }
        }   
    }
}
  • 1
    1- Don't call setSize, that will be determined by the HTML tag; 2- Don't call `setVisible`, it's visible by default; 3- It's 2014, I think you can leave `Applet` behind and graduate to `JApplet`, in fact, I'd discourage from even using that, applets have a ton of problems that come with them which makes learning with them a complete pain...IMHO – MadProgrammer Dec 07 '14 at 03:49
  • The reason it's failing to initialise is because your `buildPanel` is generating a `NullPointerException` at `mainPanel.add(titleText, BorderLayout.PAGE_START);` which suggests to me that you haven't initialized the `mainPanel`...or a lot of things in fact... – MadProgrammer Dec 07 '14 at 03:52
  • Here's an opportunity in learning how to debug. If you're not running this code within a IDE, then I highly recommend that you do so, you can then attach a breakpoint at the start of the `buildPanel` method and step through the code and check each variable before it's used. You should also show the Java console, as the applet will print useful messages to it, but so should the IDE – MadProgrammer Dec 07 '14 at 03:54
  • Oh, wow. Can't believe I didn't see that. Thanks for the help man! – Stianos Dec 07 '14 at 03:56
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Dec 08 '14 at 03:50

0 Answers0