My program compiles with no errors, but when I try to use it, the results do not show up and there is an error. Please help!!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class RetailSales extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);
// inputs
JLabel itemQuantityJLabel;
JTextField itemQuantityJTextField;
JLabel itemPriceJLabel;
JTextField itemPriceJTextField;
// outputs
JLabel subTotalResultsJLabel;
JTextField subTotalResultsJTextField;
JLabel salesTaxResultsJLabel;
JTextField salesTaxResultsJTextField;
JLabel totalAmountResultsJLabel;
JTextField totalAmountResultsJTextField;
// controls
JButton enterJButton;
JButton clearJButton;
JButton closeJButton;
// variables
int numberOfItems;
double pricePerItem;
double subTotal;
double salesTax;
double totalAmount;
double taxRate;
SalesCalculation salesCalculation;
DecimalFormat currency;
public RetailSales()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);
/* declarations */
// inputs
itemQuantityJLabel = new JLabel();
itemQuantityJLabel.setBounds(50, 50, 120, 20);
itemQuantityJLabel.setFont(new Font("Default", Font.PLAIN, 12));
itemQuantityJLabel.setText("Number Of Items");
itemQuantityJLabel.setForeground(black);
itemQuantityJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(itemQuantityJLabel);
itemQuantityJTextField = new JTextField();
itemQuantityJTextField.setBounds(190, 50, 75, 20);
itemQuantityJTextField.setFont(new Font("Default", Font.PLAIN, 12));
itemQuantityJTextField.setHorizontalAlignment(JTextField.CENTER);
itemQuantityJTextField.setForeground(black);
itemQuantityJTextField.setBackground(white);
itemQuantityJTextField.setEditable(true);
contentPane.add(itemQuantityJTextField);
itemPriceJLabel = new JLabel();
itemPriceJLabel.setBounds(50, 80, 120, 20);
itemPriceJLabel.setFont(new Font("Default", Font.PLAIN, 12));
itemPriceJLabel.setText("Price Per Item");
itemPriceJLabel.setForeground(black);
itemPriceJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(itemPriceJLabel);
itemPriceJTextField = new JTextField();
itemPriceJTextField.setBounds(190, 80, 75, 20);
itemPriceJTextField.setFont(new Font("Default", Font.PLAIN, 12));
itemPriceJTextField.setHorizontalAlignment(JTextField.CENTER);
itemPriceJTextField.setForeground(black);
itemPriceJTextField.setBackground(white);
itemPriceJTextField.setEditable(true);
contentPane.add(itemPriceJTextField);
// outputs
subTotalResultsJLabel = new JLabel();
subTotalResultsJLabel.setBounds(50, 140, 120, 20);
subTotalResultsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
subTotalResultsJLabel.setText("Sub Total");
subTotalResultsJLabel.setForeground(black);
subTotalResultsJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(subTotalResultsJLabel);
subTotalResultsJTextField = new JTextField();
subTotalResultsJTextField.setBounds(190, 140, 75, 20);
subTotalResultsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
subTotalResultsJTextField.setHorizontalAlignment(JTextField.CENTER);
subTotalResultsJTextField.setForeground(black);
subTotalResultsJTextField.setBackground(white);
subTotalResultsJTextField.setEditable(false);
contentPane.add(subTotalResultsJTextField);
salesTaxResultsJLabel = new JLabel();
salesTaxResultsJLabel.setBounds(50, 170, 120, 20);
salesTaxResultsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxResultsJLabel.setText("Sales Tax");
salesTaxResultsJLabel.setForeground(black);
salesTaxResultsJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(salesTaxResultsJLabel);
salesTaxResultsJTextField = new JTextField();
salesTaxResultsJTextField.setBounds(190, 170, 75, 20);
salesTaxResultsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxResultsJTextField.setHorizontalAlignment(JTextField.CENTER);
salesTaxResultsJTextField.setForeground(black);
salesTaxResultsJTextField.setBackground(white);
salesTaxResultsJTextField.setEditable(false);
contentPane.add(salesTaxResultsJTextField);
totalAmountResultsJLabel = new JLabel();
totalAmountResultsJLabel.setBounds(50, 200, 120, 20);
totalAmountResultsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountResultsJLabel.setText("Total Amount");
totalAmountResultsJLabel.setForeground(black);
totalAmountResultsJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(totalAmountResultsJLabel);
totalAmountResultsJTextField = new JTextField();
totalAmountResultsJTextField.setBounds(190, 200, 75, 20);
totalAmountResultsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountResultsJTextField.setHorizontalAlignment(JTextField.CENTER);
totalAmountResultsJTextField.setForeground(black);
totalAmountResultsJTextField.setBackground(white);
totalAmountResultsJTextField.setEditable(false);
contentPane.add(totalAmountResultsJTextField);
// controls
enterJButton = new JButton();
enterJButton.setBounds(20, 320, 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, 320, 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, 320, 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("RetailSales");
setSize( 400, 400 );
setVisible(true);
}
// main method
public static void main(String[] args)
{
RetailSales application = new RetailSales();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
try
{
numberOfItems = Integer.parseInt(itemQuantityJTextField.getText());
numberOfItemsNotZero();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Number of Items!",
"Number Format Error", JOptionPane.ERROR_MESSAGE);
itemQuantityJTextField.setText("");
itemQuantityJTextField.requestFocusInWindow();
}
}
public void numberOfItemsNotZero()
{
if(numberOfItems == 0)
{
JOptionPane.showMessageDialog(this,
"Please enter Number of Items greater than zero!",
"Number Format Error", JOptionPane.ERROR_MESSAGE);
itemQuantityJTextField.setText("");
itemQuantityJTextField.requestFocusInWindow();
}
else
{
getPricePerItem();
}
}
public void getPricePerItem()
{
try
{
pricePerItem = Double.parseDouble(itemPriceJTextField.getText());
pricePerItemNotZero();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please Price Per Item!",
"Number Format Error", JOptionPane.ERROR_MESSAGE);
itemPriceJTextField.setText("");
itemPriceJTextField.requestFocusInWindow();
}
}
public void pricePerItemNotZero()
{
if(pricePerItem == 0)
{
JOptionPane.showMessageDialog(this,
"Please enter second number greater than zero!",
"Number Format Error", JOptionPane.ERROR_MESSAGE);
itemPriceJTextField.setText("");
itemPriceJTextField.requestFocusInWindow();
}
else
{
getCalculations();
}
}
public void getCalculations()
{
getResults();
displayResults();
}
public void getResults()
{
salesCalculation = new SalesCalculation(numberOfItems, pricePerItem);
subTotal = salesCalculation.calculateSubTotal();
salesTax = salesCalculation.calculateSalesTax();
totalAmount = salesCalculation.calculateTotalAmount();
}
public void displayResults()
{
subTotalResultsJTextField.setText("" + currency.format(subTotal));
salesTaxResultsJTextField.setText("" + currency.format(salesTax));
totalAmountResultsJTextField.setText("" + currency.format(totalAmount));
}
public void clearJButtonActionPerformed(ActionEvent event)
{
itemQuantityJTextField.setText("");
itemQuantityJTextField.requestFocusInWindow();
itemPriceJTextField.setText("");
subTotalResultsJTextField.setText("");
salesTaxResultsJTextField.setText("");
totalAmountResultsJTextField.setText("");
}
public void closeJButtonActionPerformed(ActionEvent event)
{
RetailSales.this.dispose();
}
}
class SalesCalculation
{
int firstNumber;
double secondNumber;
double taxRate;
public SalesCalculation(int numberOfItems, double pricePerItem)
{
firstNumber = numberOfItems;
secondNumber = pricePerItem;
taxRate = .07;
}
public double calculateSubTotal()
{
return firstNumber * secondNumber;
}
public double calculateSalesTax()
{
return firstNumber * secondNumber * taxRate;
}
public double calculateTotalAmount()
{
return (firstNumber * secondNumber * taxRate) + (firstNumber * secondNumber);
}
}
Here is the error that comes up after I hit enter:
----jGRASP exec: java RetailSales
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at RetailSales.displayResults(RetailSales.java:306)
at RetailSales.getCalculations(RetailSales.java:292)
at RetailSales.pricePerItemNotZero(RetailSales.java:285)
at RetailSales.getPricePerItem(RetailSales.java:261)
at RetailSales.numberOfItemsNotZero(RetailSales.java:252)
at RetailSales.enterJButtonActionPerformed(RetailSales.java:228)
at RetailSales$1.actionPerformed(RetailSales.java:170)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)