Ok this is my first multi-class program... The output needs to be in a currency format but when I try I get the error(Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at GratuityCalculator.getGratuity(GratuityCalculator.java:186) So I know where the error is but cant figure out why. here is the code that throws the error its line 186 if I use the currency.format code I get an error if not no error but no currency either:
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;
}
}
Any help appreciated. let me know if more info is required.