- #1
clook
- 35
- 0
here is my code
I am trying to add items via a textfield and the clicking of "varietyButton" in the addVariety() method.
In the method, I am trying to implement a for loop that will compare the inputted string with all the items in the array that is in the bagelJComboBox.
Here is the code for the method
I always get a NullPointerException whenever I try to add the item. What exactly is wrong?
The remove method deleteVariety() works fine. However, I am trying to make it only execute when the user has clicked and selected a variety, as opposed to the default item ("Plain")
I have implemented an actionListener to bagelJComboBox for this purpose and even implemented it in the actionPerformed method, but it still does not work. What gives?
Code:
import javax.swing.*;
import java.text.*;
import java.awt.event.*;
public class Ch6Bagel extends JFrame implements ActionListener, ItemListener {
//create GUI objects
JPanel mainPanel = new JPanel();
JTextField bagelQuantityTextField = new JTextField(10);
JTextField creamCheeseQuantityTextField = new JTextField(10);
JTextField newVarietyTextField = new JTextField(10);
JCheckBox checkBox = new JCheckBox("Add Cream Cheese");
JButton orderButton = new JButton("Order");
JButton clearButton = new JButton("Clear");
JButton summaryButton = new JButton("Summary");
JButton varietyButton = new JButton("Add Variety");
JButton deleteVarietyButton = new JButton("Delete Variety");
JLabel bagelLabel = new JLabel("Lee Bagels");
JTextArea outputTextArea = new JTextArea("Your Total Cost:", 10, 15);
//datatypes and objects
String[] bagelString = {"Plain", "Egg", "Rye", "Salt", "Blueberry",
"Garlic", "Onion", "Sesame", "Poppy Seed", "The Works"};
String[] creamCheeseString = {"Plain", "Herb", "Garlic"};
int quantityBagelInteger, quantityCreamCheeseInteger, counterInteger = 1;
String bagelTypeString, creamCheeseTypeString, addVarietyString;
//create comboboxes
JComboBox bagelJComboBox = new JComboBox(bagelString);
JComboBox creamCheeseJComboBox = new JComboBox(creamCheeseString);
public static void main(String[] args)
{
Ch6Bagel myBagel = new Ch6Bagel();
myBagel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Ch6Bagel()
{
designFrame();
}
public void designFrame()
{
creamCheeseJComboBox.setEnabled(false);
creamCheeseQuantityTextField.setEditable(false);
mainPanel.add(bagelLabel);
mainPanel.add(new JLabel(" Type of Bagel "));
mainPanel.add(bagelJComboBox);
mainPanel.add(new JLabel(" Quantity "));
mainPanel.add(bagelQuantityTextField);
mainPanel.add(checkBox);
mainPanel.add(new JLabel(" Type of Cream Cheese "));
mainPanel.add(creamCheeseJComboBox);
mainPanel.add(new JLabel(" Quantity "));
mainPanel.add(creamCheeseQuantityTextField);
mainPanel.add(outputTextArea);
mainPanel.add(orderButton);
mainPanel.add(summaryButton);
mainPanel.add(varietyButton);
mainPanel.add(newVarietyTextField);
mainPanel.add(deleteVarietyButton);
mainPanel.add(clearButton);
//add listeners
orderButton.addActionListener(this);
summaryButton.addActionListener(this);
checkBox.addItemListener(this);
clearButton.addActionListener(this);
varietyButton.addActionListener(this);
deleteVarietyButton.addActionListener(this);
bagelJComboBox.addActionListener(this);
//add the panel to the frame
add(mainPanel);
setSize(200,600);
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
//retrieve the logical of the component that fired the action event
Object sourceObject = evt.getSource();
if(sourceObject == orderButton)
{
counterInteger++;
getInput();
displayOutput();
}
else if(sourceObject == summaryButton)
{
summary();
}
else if(sourceObject == varietyButton)
{
addVariety();
}
else if(sourceObject == deleteVarietyButton && sourceObject == bagelJComboBox)
{
deleteVariety();
}
else if(sourceObject == clearButton)
{
clear();
}
}
public void itemStateChanged(ItemEvent evt)
{
if(checkBox.isSelected())
{
creamCheeseJComboBox.setEnabled(true);
creamCheeseQuantityTextField.setEditable(true);
}
else
{
creamCheeseJComboBox.setEnabled(false);
creamCheeseQuantityTextField.setEditable(false);
}
}
public void getInput()
{
bagelTypeString = (String) bagelJComboBox.getSelectedItem();
creamCheeseTypeString = (String) creamCheeseJComboBox.getSelectedItem();
if (bagelQuantityTextField.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Please enter a number!");
}
else
{
quantityBagelInteger = Integer.parseInt(bagelQuantityTextField.getText());
}
if (creamCheeseQuantityTextField.getText().length() == 0 && checkBox.isSelected())
{
JOptionPane.showMessageDialog(null, "Please enter a number!");
}
else
{
quantityCreamCheeseInteger = Integer.parseInt(creamCheeseQuantityTextField.getText());
}
}
public void displayOutput()
{
DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");
double bagelCostDouble, creamCheeseCostDouble, totalCostDouble;
Calculate myBagelCost = new Calculate(quantityBagelInteger, quantityCreamCheeseInteger, counterInteger);
bagelCostDouble = myBagelCost.getBagelCost();
creamCheeseCostDouble = myBagelCost.getCheeseCost();
totalCostDouble = myBagelCost.getTotalCost();
outputTextArea.setText("Type of Bagel:" + bagelTypeString +
'\n' + "Quantity: " + quantityBagelInteger +
'\n' + "Cream Cheese: " + creamCheeseTypeString +
'\n' + "Quantity: " + quantityCreamCheeseInteger +
'\n' + "Bagels: " + formatDecimalFormat.format(bagelCostDouble) +
'\n' + "Cream Cheese: " + formatDecimalFormat.format(creamCheeseCostDouble)
+ '\n' + "Total Cost: " + formatDecimalFormat.format(totalCostDouble));
}
public void summary()
{
DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");
double totalBagelCostDouble, totalCreamCheeseCostDouble, averageBagelsBoughtDouble;
Calculate myBagelCost = new Calculate(quantityBagelInteger, quantityCreamCheeseInteger, counterInteger);
totalBagelCostDouble = myBagelCost.getTotalBagelCost();
totalCreamCheeseCostDouble = myBagelCost.getTotalCheeseCost();
averageBagelsBoughtDouble = myBagelCost.getAverageBagels();
JOptionPane.showMessageDialog(null,
'\n' + " Total Cost of Bagels " + formatDecimalFormat.format(totalBagelCostDouble)+
'\n' + " Total Cost of Cream Cheese " + formatDecimalFormat.format(totalCreamCheeseCostDouble)
+ '\n' + "Average Number of Bagels Bought" + averageBagelsBoughtDouble);
}
public void addVariety()
{
int i = 0;
for (i = 0; i < 9; i++)
{
if (addVarietyString.equals(bagelString[i]))
JOptionPane.showMessageDialog(null, "Duplicate Variety!");
else
addVarietyString = newVarietyTextField.getText();
bagelJComboBox.insertItemAt(addVarietyString, 10);
}
}
public void deleteVariety()
{
bagelJComboBox.removeItemAt(bagelJComboBox.getSelectedIndex());
}
public void clear()
{
bagelQuantityTextField.setText("");
creamCheeseQuantityTextField.setText("");
newVarietyTextField.setText("");
}
}
I am trying to add items via a textfield and the clicking of "varietyButton" in the addVariety() method.
In the method, I am trying to implement a for loop that will compare the inputted string with all the items in the array that is in the bagelJComboBox.
Here is the code for the method
Code:
public void addVariety()
{
addVarietyString = newVarietyTextField.getText();
int i = 0;
for (i = 0; i < 9; i++)
{
if (addVarietyString.equals(bagelString[i]))
JOptionPane.showMessageDialog(null, "Duplicate Variety!");
else
bagelJComboBox.addItem(addVarietyString);
}
}
I always get a NullPointerException whenever I try to add the item. What exactly is wrong?
The remove method deleteVariety() works fine. However, I am trying to make it only execute when the user has clicked and selected a variety, as opposed to the default item ("Plain")
I have implemented an actionListener to bagelJComboBox for this purpose and even implemented it in the actionPerformed method, but it still does not work. What gives?