GratuityCalculator.getGratuity中的线程“AWT-EventQueue-0”java.lang.NullPointerException中的异常(GratuityCalculator.java:188)

时间:2015-03-30 19:11:27

标签: java nullpointerexception

好吧这是我的第一个多类程序......输出需要采用货币格式,但是当我尝试时我得到错误(线程中的异常" AWT-EventQueue-0" java。 lang.NullPointerException 在GratuityCalculator.getGratuity(GratuityCalculator.java:186) 所以我知道错误在哪里,但无法弄清楚原因。 如果我使用currency.format代码,这里是抛出错误的代码186,如果不是没有错误但没有货币,我会收到错误:

public class GratuityCalculator extends JFrame
{
/* declarations */

// color objects
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);

DecimalFormat currency;

//components
JLabel billAmountJLabel;
JTextField billAmountJTextField;

JLabel gratuityJLabel;
JTextField gratuityJTextField;

JButton enterJButton;
JButton clearJButton;
JButton closeJButton;

// variables
double billAmount;
final double GRATUITY_RATE = .15;
double gratuityAmount;

// objects
CalculateTip calculateTip;// object class

public GratuityCalculator()
{
    createUserInterface();
}

public void createUserInterface()
{
    Container contentPane = getContentPane();
    contentPane.setBackground (Color.white);
    contentPane.setLayout(null);

    //initialize components
    billAmountJLabel = new JLabel();
    billAmountJLabel.setBounds(50, 50, 120, 20);
    billAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    billAmountJLabel.setText("Enter bill amount");
    billAmountJLabel.setForeground(black);
    billAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(billAmountJLabel);

    billAmountJTextField = new JTextField();
    billAmountJTextField.setBounds(225, 50, 50, 20);
    billAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    billAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
    billAmountJTextField.setForeground(black);
    billAmountJTextField.setBackground(white);
    billAmountJTextField.setEditable(true);
    contentPane.add(billAmountJTextField);

    gratuityJLabel = new JLabel();
    gratuityJLabel.setBounds(50, 80, 150, 20);
    gratuityJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    gratuityJLabel.setText("Gratuity");
    gratuityJLabel.setForeground(black);
    gratuityJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(gratuityJLabel);

    gratuityJTextField = new JTextField();
    gratuityJTextField.setBounds(225, 80, 50, 20);
    gratuityJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    gratuityJTextField.setHorizontalAlignment(JTextField.CENTER);
    gratuityJTextField.setForeground(black);
    gratuityJTextField.setBackground(white);
    gratuityJTextField.setEditable(false);
    contentPane.add(gratuityJTextField);

    enterJButton = new JButton();
    enterJButton.setBounds(20, 300, 100, 20);
    enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    enterJButton.setText("Enter");
    enterJButton.setForeground(black);
    enterJButton.setBackground(white);
    contentPane.add(enterJButton);
    enterJButton.addActionListener(

        new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                enterJButtonActionPerformed(event);
            }
        }
    );

    clearJButton = new JButton();
    clearJButton.setBounds(140, 300, 100, 20);
    clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
    clearJButton.setText("Clear");
    clearJButton.setForeground(black);
    clearJButton.setBackground(white);
    contentPane.add(clearJButton);
    clearJButton.addActionListener(

        new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                clearJButtonActionPerformed(event);
            }
        }
    );

    closeJButton = new JButton();
    closeJButton.setBounds(260, 300, 100, 20);
    closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
    closeJButton.setText("Close");
    closeJButton.setForeground(black);
    closeJButton.setBackground(white);
    contentPane.add(closeJButton);
    closeJButton.addActionListener(

        new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                closeJButtonActionPerformed(event);
            }
        }
    );

    setTitle("Gratuity Calculator");
    setSize( 400, 400 );
    setVisible(true);
}

//main method
public static void main(String[] args)
{
    GratuityCalculator application = new GratuityCalculator();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void enterJButtonActionPerformed(ActionEvent event)
{
    getBillAmount();
}

public void getBillAmount()
{
    try
    {
        billAmount = Double.parseDouble(billAmountJTextField.getText());
        getGratuity();
    }
    catch(NumberFormatException exception)
    {
        JOptionPane.showMessageDialog(this,
        "Please Enter Bill Amount!",
        "Number Format Error", JOptionPane.ERROR_MESSAGE);
        billAmountJTextField.setText("");
        billAmountJTextField.requestFocusInWindow();
    }
}

public void getGratuity()
{
    /*
    Object class is created and gratuity calculated
    */
    calculateTip = new CalculateTip(billAmount, GRATUITY_RATE);

    /*
    Call object class
    */
    gratuityAmount = calculateTip.getGratuity();
    gratuityJTextField.setText("" + currency.format(gratuityAmount));
}

public void clearJButtonActionPerformed(ActionEvent event)
    {
        billAmountJTextField.setText("");
        billAmountJTextField.requestFocusInWindow();
        gratuityJTextField.setText("");
    }

    public void closeJButtonActionPerformed(ActionEvent event)
    {
        GratuityCalculator.this.dispose();
    }
}

class CalculateTip
{
    // class variables
    double firstNumber;
    double secondNumber;


    /*
    The object class constructor receives the two values
    from the interface class and the two parameter values
    are assigned to the class variables
    */
    public CalculateTip(double billAmount, double GRATUITY_RATE)
    {
        firstNumber = billAmount;
        secondNumber = GRATUITY_RATE;
    }

    /*
    This method returns the sum of the values to the class
    */
    public double getGratuity()
    {
        return firstNumber * secondNumber;
    }

}

任何帮助表示赞赏。如果需要更多信息,请与我们联系。

0 个答案:

没有答案