I created a java Applet. It has an error. The error is applet not initialized.
I need urgent help for this error. I created the interface using netbeings & coppied the code to the Jcreater. :(

Recommended Answers

All 5 Replies

You need to post your code so we can help.

This is my code.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;


public class soda extends Applet implements ActionListener, ItemListener {


Button b1,b2,b3,b4,b5,b6,b7;
Choice choice1;
Checkbox ch1,ch2,ch3,ch4,ch5;
double num=0,money;
CheckboxGroup cbg;


public void init() {
Label lblhead = new Label();
cbg = new CheckboxGroup();
Checkbox    ch1 = new Checkbox("COLA", cbg, false);
Checkbox    ch2 = new Checkbox("ORANGE", cbg, false);
Checkbox    ch3 = new Checkbox("LIME", cbg, false);
Button    b1 = new Button();
Button    b2 = new Button();
Button    b3 = new Button();
Button    b4 = new Button();
Button    b5 = new Button();
Button    b6 = new Button();
Button    b7 = new Button();
choice1.add("10 oz");
choice1.add("16 oz");
Choice    choice1 = new Choice();
Label    lblsize = new Label();
Label    lbltype = new Label();
Checkbox    ch4 = new  Checkbox();
Checkbox    ch5 = new Checkbox();
Label    lblamt = new Label();
TextField    txtamt = new TextField();
Button    btnbal = new Button();
TextField    txtbal = new TextField();
Button    btnref = new Button();
Label    lblfoot = new Label();


setBackground(new Color(255, 153, 153));
setLayout(null);


lblhead.setAlignment(Label.CENTER);
lblhead.setFont(new Font("Calibri", 1, 18)); // NOI18N
lblhead.setText("Soda Vending Machine");
add(lblhead);
lblhead.setBounds(80, 30, 230, 30);



add(ch1);
ch1.setBounds(60, 70, 58, 20);



add(ch2);
ch2.setBounds(60, 100, 77, 20);



add(ch3);
ch3.setBounds(60, 130, 53, 20);


b1.setBackground(new Color(0, 153, 153));
b1.setLabel("RM 00.10");
add(b1);
b1.setBounds(320, 60, 70, 24);


b2.setBackground(new Color(0, 153, 153));
b2.setLabel("RM 00.20");
add(b2);
b2.setBounds(320, 100, 68, 24);


b3.setBackground(new Color(0, 153, 153));
b3.setLabel("RM 00.50");
add(b3);
b3.setBounds(320, 140, 68, 24);


b4.setBackground(new Color(0, 153, 153));
b4.setLabel("RM 1.00");
add(b4);
b4.setBounds(320, 180, 70, 24);


b5.setBackground(new Color(0, 153, 153));
b5.setLabel("RM 2.00");
add(b5);
b5.setBounds(320, 220, 70, 24);


b6.setBackground(new Color(0, 153, 153));
b6.setLabel("RM 5.00");
add(b6);
b6.setBounds(320, 260, 70, 24);


b7.setBackground(new Color(0, 153, 153));
b7.setLabel("RM 10.00");
add(b7);
b7.setBounds(320, 300, 70, 24);
add(choice1);
choice1.setBounds(200, 170, 90, 20);


lblsize.setText("SIze");
add(lblsize);
lblsize.setBounds(60, 170, 70, 20);


lbltype.setText("Type");
add(lbltype);
lbltype.setBounds(60, 200, 30, 20);


ch4.setLabel("Regular");
add(ch4);
ch4.setBounds(200, 200, 84, 20);


ch5.setLabel("Diet");
add(ch5);
ch5.setBounds(200, 230, 48, 20);


lblamt.setText("Total Amount");
add(lblamt);
lblamt.setBounds(60, 270, 76, 20);
add(txtamt);
txtamt.setBounds(200, 270, 90, 20);


btnbal.setBackground(new Color(0, 153, 153));
btnbal.setLabel("Balance");
add(btnbal);
btnbal.setBounds(60, 300, 61, 24);
add(txtbal);
txtbal.setBounds(200, 300, 90, 20);


btnref.setBackground(new Color(0, 153, 153));
btnref.setLabel("Take Soda and Refresh");
add(btnref);
btnref.setBounds(60, 350, 320, 24);


lblfoot.setAlignment(Label.CENTER);
lblfoot.setFont(new Font("Calibri", 1, 14));
lblfoot.setText("Thank You & Come Again!!!");
add(lblfoot);


ch1.addItemListener(this);
ch2.addItemListener(this);
ch3.addItemListener(this);


b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
}
public void itemStateChanged(ItemEvent ie) {


if(ie.getItemSelectable()==ch1)
{
setBackground(Color.red);
}
if(ie.getItemSelectable()==ch2)
{
setBackground(Color.orange);
}
if(ie.getItemSelectable()==ch3)
{
setBackground(Color.green);
}


}


public void actionPerformed(ActionEvent e) {


if(e.getSource()==b1)
money=0.10;


else if(e.getSource()==b2)
money=0.20;


else if(e.getSource()==b3)
money=0.50;


else if(e.getSource()==b4)
money=1.00;


else if(e.getSource()==b5)
money=2.00;


else if(e.getSource()==b6)
money=5.00;


else if(e.getSource()==b7)
money=10.00;


num=num+money;



}


}

You need to instantiate choice1 before you add the string item. So change

choice1.add("10 oz");
choice1.add("16 oz");
Choice choice1 = new Choice();

to

Choice choice1 = new Choice();
choice1.add("10 oz");
choice1.add("16 oz");
commented: Nice of you to help him +10

OK. It's work. Thanks alot.
Thank u.

Be aware that you have re-declared all of your class-level component variables in your init() method, so if you refer to any of those outside of init() you'll find they are null and none of your listeners will work as you expect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.