I have this project in Java class and I chose to make a supermarket delivery service it provides the following:
- the products and their prices.
- The ability to choose the amount of products.
- The user gives his phone number, so the delivery man can contact them for direction.
- The user can enter a discount code.
- The user can cancel the order or confirm it, when the user confirms, the bill is printed with a note that the order will arrive soon.
- The user can enter the amount of money they will give the delivery, so the delivery man can bring the change.
I fixed this code a million times and I still get stupid mistakes because I have a blind eye for the mistakes
my mistake this time is as the netBeans told me
Exception in thread "main" java.lang.StackOverflowError
at java.awt.Insets.<init>(Insets.java:103)
at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
at sun.awt.windows.WToolkit.getScreenInsets(WToolkit.java:580)
at java.awt.Window.init(Window.java:498)
at java.awt.Window.<init>(Window.java:536)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at javax.swing.JFrame.<init>(JFrame.java:180)
at supermarket.SuperMarket.<init>(SuperMarket.java:61)
at supermarket.SuperMarket.<init>(SuperMarket.java:132)
package supermarket;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class SuperMarket extends JFrame implements ActionListener {
double juice = 1, tuna = 4, bread = 7, shampoo = 25;
double discount = 0, subTotal = 0, total = 0, d=0;
double jnum=0, tnum=0, bnum=0, snum=0;
double shN, brN, tuN, juN;
double shd, brd, tud, jud;
private JTextField shField;
private JTextField brField;
private JTextField tuField;
private JTextField juField;
private JButton cancelButton;
private JButton confirmButton;
private JTextField nmField;
private JLabel shLabel;
private JLabel brLabel;
private JLabel tuLabel;
private JLabel juLabel;
private JLabel shAmount;
private JLabel brAmount;
private JLabel tuAmount;
private JLabel juAmount;
private JPanel buttonPanel;
private JLabel shError;
private JLabel brError;
private JLabel tuError;
private JLabel juError;
private SuperMarket mainFrm;
private SuperMarket childFrm;
/**
* Create the order form.
*/
public SuperMarket() {
shLabel=new JLabel ("Shampoo Price: 25 ",SwingConstants.LEFT);
shField = new JTextField(10);
shAmount = new JLabel(shLabel+"Amount: "+shField);
try{
shd = Double.parseDouble(shField.getText());
}
catch(NumberFormatException shE){
shField.setToolTipText("You must enter integers.");
shField.setText("");
}
shN = (int) shd;
snum=shN*shampoo;
brLabel=new JLabel ("Bread Price: 7 ",SwingConstants.LEFT);
brField = new JTextField(10);
brAmount= new JLabel(brLabel+"Amount: "+brField);
try{
brd = Double.parseDouble(brField.getText());
}
catch(NumberFormatException shE){
brField.setToolTipText("You must enter integers.");
brField.setText("");
}
brN = (int) brd;
bnum=brN*bread;
tuLabel=new JLabel ("Tuna Price: 4 ",SwingConstants.LEFT);
tuField = new JTextField(10);
tuAmount = new JLabel(tuLabel+"Amount: "+tuField);
try{
tud = Double.parseDouble(tuField.getText());
}
catch(NumberFormatException shE){
tuField.setToolTipText("You must enter integers.");
tuField.setText("");
}
tuN = (int) tud;
tnum=tuN*tuna;
juLabel=new JLabel ("Juice Price: 1 ",SwingConstants.LEFT);
juField = new JTextField(10);
juAmount = new JLabel(juLabel+"Amount: "+juField);
try{
jud = Double.parseDouble(juField.getText());
}
catch(NumberFormatException shE){
juField.setToolTipText("You must enter integers.");
juField.setText("");
}
juN = (int) jud;
jnum=juN*juice;
// Order and cancel buttons
buttonPanel = new JPanel();
confirmButton = new JButton("Confirm");
buttonPanel.add(confirmButton);
cancelButton = new JButton("Cancel order");
buttonPanel.add(cancelButton);
mainFrm=new SuperMarket();
confirmButton.addActionListener(this);
cancelButton.addActionListener(this);
add(shLabel);
add(shAmount);
add(tuLabel);
add(tuAmount);
add(brLabel);
add(brAmount);
add(juLabel);
add(juAmount);
add(buttonPanel);
setVisible(true);
pack();
}
public static void main(String[] args) {
SuperMarket market=new SuperMarket();
market.setLayout(new FlowLayout());
market.setTitle("AHL Supermarket Delivery Service");
market.setSize(800, 700);
market.setDefaultCloseOperation(EXIT_ON_CLOSE);
market.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent buttonEvent) {
if (buttonEvent.getSource() == confirmButton) {
childFrm=new SuperMarket();
JButton dnBtn=new JButton("Done");
childFrm.setLayout(new FlowLayout());
childFrm.setSize(200, 200);
JPanel nmPanel = new JPanel();
nmPanel.add(new JLabel("Name: "));
nmField = new JTextField(10);
nmPanel.add(nmField);
childFrm.add(nmPanel);
childFrm.add(dnBtn);
dnBtn.addActionListener(this);
childFrm.setVisible(true);
}
clearForm();
}
/**
* Clear all entries in the order form.
*/
private void clearForm() {
shField.setText("");
tuField.setText("");
juField.setText("");
}
}
I still haven't added the output nor the total and discount but I will deal with that when my current problem is fixed.
If anyone could help me I would appreciate it very much because I got completely frustrated.
( you could tell from the amount of unnecessary variables :[ )
You're creating a new instance of the same class in the constructor.
mainFrm=new SuperMarket();
This means you will be calling the constructor again, which will create a new instance, which will call the constructor again, and so on until you run out of "memory" (stack space in this case, hence the StackOverflowError).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With