小程序不会计算

时间:2014-02-21 07:58:48

标签: java nullpointerexception applet awt

我这里有一个applet程序,允许用户从下拉列表中选择一个电影然后选择一些票。应显示一个复选框供用户点击,以便他们可以获得matinee电影的折扣。用户应该能够单击计算价格按钮以显示总成本,并单击清除按钮以清除每个下拉列表和复选框。该程序已编译但当我单击计算按钮时,它生成了很多错误代码,如java.lang.nullPointerException等等。我只是希望程序正常工作所以请帮助我。请参考以下代码:

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

public class Proj4exe2 extends Applet implements ItemListener, ActionListener
{
Choice movieChoice = new Choice();
Button calcButton = new Button("CALCULATE");
Choice ticketsChoice = new Choice();
Button clearButton = new Button("CLEAR");
Checkbox matineeBox = new Checkbox("Matinee Discount", false);
Label outputLabel = new Label("");
int i = 1;
int size;
double presyoKo[];
double totalPresyo = 0.00;
double diskwento;

public void init()
{
setBackground(Color.yellow);
add(new Label("Welcome to ENCORE MOVIES"));
add(new Label("Please choose a movie:"));
add(movieChoice);
movieChoice.addItem("My Little Bossings");
movieChoice.addItem("Pagpag");
movieChoice.addItem("San Pedro Calungsod");
movieChoice.addItem("Kimi Dora Prequel");
movieChoice.addItem("10,000 Hours");
    movieChoice.addItem("Girl, Boy, Bakla, Tomboy");
    movieChoice.addItem("Boy Golden");
    movieChoice.addItem("Kaleidoscope World");
movieChoice.addItemListener(this);
add(calcButton);
calcButton.addActionListener(this);
add(ticketsChoice);
ticketsChoice.addItem("0");
ticketsChoice.addItem("1");
ticketsChoice.addItem("2");
ticketsChoice.addItem("3");
ticketsChoice.addItem("4");
ticketsChoice.addItem("5");
ticketsChoice.addItem("6");
ticketsChoice.addItem("7");
ticketsChoice.addItem("8");
ticketsChoice.addItem("9");
ticketsChoice.addItem("10");
ticketsChoice.addItem("11");
ticketsChoice.addItem("12");
ticketsChoice.addItem("13");
ticketsChoice.addItem("14");
ticketsChoice.addItem("15");
ticketsChoice.addItemListener(this);
add(clearButton);
clearButton.addActionListener(this);
add(matineeBox);
matineeBox.addItemListener(this);
add(outputLabel);
}
public void actionPerformed(ActionEvent e)
{
    String args = e.getActionCommand();
    if(args == "CALCULATE")
    {

        size = i++;
        presyoKo[i] = Double.parseDouble(ticketsChoice.getSelectedItem());

        totalPresyo = presyoKo[i] * 50.0;
        outputLabel.setText("Your charge is " + totalPresyo + ". Thank you.");
        }
        if(args == "CLEAR")
        {
            movieChoice.select(0);
            ticketsChoice.select(0);
            matineeBox.setState(false);
            outputLabel.setText("");
            }
    }
    public void itemStateChanged(ItemEvent ee)
    {
        diskwento = (totalPresyo * .25);
        if(matineeBox.getState() == true)
        {
            totalPresyo = totalPresyo - diskwento;
            }
        }
}

小程序代码:

<html>
<applet code = "Proj4exe2.java" height = "450" width = "450">
</applet code>
</html>

请原谅我笨拙的设计,因为我没有创建布局管理器。我非常欢迎提示和评论,以进一步改进该计划的代码。非常感谢你!

1 个答案:

答案 0 :(得分:1)

double presyoKo[];在初始化之前用于actionPerformed

修改

不使用数组而是使用ArrayList并继续添加

ArrayList<Double> ticketPrices = new ArrayList<Double>();
...
... actionPerformed(...) ...
  double qty = Double.parseDouble(ticketsChoice.getSelectedItem());
  ticketPrices.add(qty * 50);