The following code is for a standalone applet that calculates a shipping rate based on a selected shipping method. Normally, the program would read the total price of purchases from the website but for the sake of this exercise, the user will enter the price on the applet.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class CandleLineApplet extends Applet implements ItemListener
{
//declare variables and construct a color
double orderAmount, rate;
int rateCode;
//create components for applet
Label companyNameLabel = new Label("CandleLine -- Candles Online");
Label promptLabel = new Label("Please enter the total dollar amount of your order");
TextField salesField = new TextField(20);
Label shippingMethodLabel = new Label("Please choose your method of shipping:");
CheckboxGroup rateGroup = new CheckboxGroup();
Checkbox priorityBox = new Checkbox("Priority (Overnight)", false, rateGroup);
Checkbox expressBox = new Checkbox("Express (2 business days)", false, rateGroup);
Checkbox standardBox = new Checkbox("Standard (3 to 7 business days)", false, rateGroup);
Checkbox hiddenBox = new Checkbox("", false, rateGroup);
Label sloganLabel = new Label("We guarantee on time delivery, or your money back");
Label outputLabel;
public void init()
{
setBackground(Color.green);
setForeground(Color.black);
add(companyNameLabel);
add(promptLabel);
add(salesField);
salesField.requestFocus();
salesField.setForeground(Color.black);
add(shippingMethodLabel);
add(priorityBox);
priorityBox.addItemListener(this);
add(expressBox);
expressBox.addItemListener(this);
add(standardBox);
standardBox.addItemListener(this);
add(sloganLabel);
}
public void itemStateChanged(ItemEvent rateChoice)
{
try
{
orderAmount = getOrderAmount();
rateCode = getRateCode();
rate = calculateRate(rateCode, orderAmount);
displayResult(orderAmount, rate);
}
catch (NumberFormatException e)
{
outputLabel.setText("You must enter a dollar amount greater than zero.");
hiddenBox.setState(true);
salesField.setText("");
salesField.requestFocus();
}
}
public double getOrderAmount()
{
double order = Double.parseDouble(salesField.getText());
if (order <= 0.00) throw new NumberFormatException();
return order;
}
public int getRateCode()
{
int code = 0;
if (priorityBox.getState()) code = 1;
else
if (expressBox.getState()) code = 2;
else
if (standardBox.getState()) code = 3;
return code;
}
public double calculateRate(int rateCode, double totalOrderCost )
{
double shippingRate = 0.00;
switch (rateCode)
{
case 1:
shippingRate = 16.95;
break;
case 2:
shippingRate = 13.95;
break;
case 3:
if (totalOrderCost < 100.00)
shippingRate = 7.95;
break;
}
return shippingRate;
}
public void displayResult(double orderTotal, double shippingRate )
{
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
outputLabel.setText("Your shipping charge for your order of " + twoDigits.format(orderTotal) + " is " + twoDigits.format(shippingRate));
}
}
The problem here is that this application is throwing a NullPointerException when itemStateChanged() is invoked - been struggling for a week now to debug it but had no success.
This is the error message that was generated at runtime:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at CandleLineApplet.itemStateChanged(CandleLineApplet.java:67)
at java.awt.Checkbox.processItemEvent(Checkbox.java:559)
at java.awt.Checkbox.processEvent(Checkbox.java:526)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
PS; If this can be coded any simpler, I would really appreciate any suggestions.
Thanks