fix my code Exception in thread "AWT-EventQueue-0"…
- User
fix my code Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1: import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.border.TitledBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import javax.swing.border.BevelBorder; import javax.swing.border.LineBorder; import javax.swing.border.SoftBevelBorder; import java.awt.GridLayout; import javax.swing.ImageIcon; import javax.swing.JComboBox; import javax.swing.DefaultComboBoxModel; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.awt.event.ActionEvent; import java.util.ArrayList;
public class PizzaOrder extends JFrame {
private JPanel contentPane; private JTextField NumPizza; private JTextField NumTopping; private JTextField NumBreuvage; private JButton Ajouter; private double totales; private Map<String, Double> History; private static String previousHistory; private List<String> historyList; private Map<String, Double> historyMap; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { PizzaOrder frame = new PizzaOrder(new HashMap<>()); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public PizzaOrder(Map<String, Double> previousHistory) { // First use the existing history products and total them totales = previousHistory.values().stream().reduce(0.0, Double::sum); History = previousHistory; historyList = new ArrayList<>(History.keySet()); historyMap = new HashMap<>(History); setTitle("Order"); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 630, 689); contentPane = new JPanel(); contentPane.setBackground(new Color(255, 147, 0)); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel Menu = new JPanel(); Menu.setBackground(new Color(255, 147, 0)); Menu.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Menu", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); Menu.setBounds(6, 6, 618, 158); contentPane.add(Menu); Menu.setLayout(new GridLayout(0, 3, 0, 0)); JPanel PizzaPrice = new JPanel(); PizzaPrice.setBackground(new Color(255, 147, 0)); PizzaPrice.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Pizza", TitledBorder.LEADING, TitledBorder.TOP, null, null)); Menu.add(PizzaPrice); PizzaPrice.setLayout(null); JLabel PetitPizza = new JLabel("Petit: 6.79$"); PetitPizza.setBounds(17, 21, 72, 16); PizzaPrice.add(PetitPizza); JLabel MoyenPizza = new JLabel("Moyen: 8.29$"); MoyenPizza.setBounds(17, 40, 85, 16); PizzaPrice.add(MoyenPizza); JLabel LargePizza = new JLabel("Large: 9.49$"); LargePizza.setBounds(17, 59, 85, 16); PizzaPrice.add(LargePizza); JLabel ExtraLargePizza = new JLabel("Extra Large: 10.29$"); ExtraLargePizza.setBounds(17, 78, 127, 16); PizzaPrice.add(ExtraLargePizza); JLabel FetePizza = new JLabel("Fete: 15.99$"); FetePizza.setBounds(17, 97, 93, 16); PizzaPrice.add(FetePizza); JPanel ToppingPrice = new JPanel(); ToppingPrice.setBackground(new Color(255, 147, 0)); ToppingPrice.setLayout(null); ToppingPrice.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Toppings", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); Menu.add(ToppingPrice); JLabel Petittopping = new JLabel("Petit: 1.20$"); Petittopping.setBounds(17, 21, 72, 16); ToppingPrice.add(Petittopping); JLabel Moyentopping = new JLabel("Moyen: 1.40$"); Moyentopping.setBounds(17, 40, 85, 16); ToppingPrice.add(Moyentopping); JLabel Largetopping = new JLabel("Large: 1.60$"); Largetopping.setBounds(17, 59, 85, 16); ToppingPrice.add(Largetopping); JLabel ExtraLargetopping = new JLabel("Extra Large: 1.80$"); ExtraLargetopping.setBounds(17, 78, 127, 16); ToppingPrice.add(ExtraLargetopping); JLabel Fetetopping = new JLabel("Fete: 2.30$"); Fetetopping.setBounds(17, 97, 93, 16); ToppingPrice.add(Fetetopping); JPanel BreuvagePrice = new JPanel(); BreuvagePrice.setBackground(new Color(255, 147, 0)); BreuvagePrice.setLayout(null); BreuvagePrice.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Breuvages", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); Menu.add(BreuvagePrice); JLabel Pop = new JLabel("Pop: 1.10$"); Pop.setBounds(17, 21, 72, 16); BreuvagePrice.add(Pop); JLabel Jus = new JLabel("Jus: 1.35$"); Jus.setBounds(17, 40, 85, 16); BreuvagePrice.add(Jus); JLabel Eau = new JLabel("Eau: 1.00$"); Eau.setBounds(17, 59, 85, 16); BreuvagePrice.add(Eau); JPanel PizzaSelect = new JPanel(); PizzaSelect.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null)); PizzaSelect.setBackground(new Color(255, 147, 0)); PizzaSelect.setBounds(16, 187, 350, 300); contentPane.add(PizzaSelect); PizzaSelect.setLayout(null); JComboBox<PizzaSize> ChoixPizza = new JComboBox<>(PizzaSize.values()); ChoixPizza.setModel(new DefaultComboBoxModel(PizzaSize.values())); ChoixPizza.setBounds(44, 8, 126, 27); ChoixPizza.setMaximumRowCount(5); PizzaSelect.add(ChoixPizza); NumPizza = new JTextField(); NumPizza.setText("0"); NumPizza.setBounds(175, 8, 130, 26); PizzaSelect.add(NumPizza); NumPizza.setColumns(10); JLabel PizzaIcon = new JLabel(""); PizzaIcon.setBounds(6, 6, 350, 279); PizzaSelect.add(PizzaIcon); PizzaIcon.setIcon(new ImageIcon(PizzaOrder.class.getResource("/Image/PizzaImage.png"))); JPanel ToppingSelect; ToppingSelect = new JPanel(); ToppingSelect.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null)); ToppingSelect.setBackground(new Color(255, 147, 0)); ToppingSelect.setBounds(400, 187, 208, 129); contentPane.add(ToppingSelect); ToppingSelect.setLayout(null); JComboBox<ToppingSize> ChoixTopping = new JComboBox<>(ToppingSize.values()); ChoixTopping.setModel(new DefaultComboBoxModel(ToppingSize.values())); ChoixTopping.setBounds(41, 8, 126, 27); ChoixTopping.setMaximumRowCount(5); ToppingSelect.add(ChoixTopping); NumTopping = new JTextField(); NumTopping.setText("0"); NumTopping.setBounds(39, 40, 130, 26); NumTopping.setColumns(10); ToppingSelect.add(NumTopping); JLabel ToppingIcon = new JLabel(""); ToppingIcon.setIcon(new ImageIcon(PizzaOrder.class.getResource("/Image/ToppingImage.png"))); ToppingIcon.setBounds(6, 8, 208, 109); ToppingSelect.add(ToppingIcon); JPanel BreuvageSelect = new JPanel(); BreuvageSelect.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null)); BreuvageSelect.setBackground(new Color(255, 147, 0)); BreuvageSelect.setBounds(400, 358, 208, 129); contentPane.add(BreuvageSelect); BreuvageSelect.setLayout(null); JComboBox<Breuvages> ChoixBreuvage = new JComboBox<>(Breuvages.values()); ChoixBreuvage.setModel(new DefaultComboBoxModel(Breuvages.values())); ChoixBreuvage.setBounds(64, 8, 79, 27); ChoixBreuvage.setMaximumRowCount(3); BreuvageSelect.add(ChoixBreuvage); NumBreuvage = new JTextField(); NumBreuvage.setText("0"); NumBreuvage.setBounds(39, 40, 130, 26); NumBreuvage.setColumns(10); BreuvageSelect.add(NumBreuvage); JLabel BreuvageIcon = new JLabel(""); BreuvageIcon.setIcon(new ImageIcon(PizzaOrder.class.getResource("/Image/BreuvageImage.png"))); BreuvageIcon.setBounds(0, 0, 209, 129); BreuvageSelect.add(BreuvageIcon); Ajouter = new JButton("Ajouter"); Ajouter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // retrieve selected values from combo boxes PizzaSize selectedPizzaSize = (PizzaSize) ChoixPizza.getSelectedItem(); ToppingSize selectedToppingSize = (ToppingSize) ChoixTopping.getSelectedItem(); Breuvages selectedBreuvage = (Breuvages) ChoixBreuvage.getSelectedItem(); int numPizza = Integer.parseInt(NumPizza.getText()); int numTopping = Integer.parseInt(NumTopping.getText()); int numBreuvage = Integer.parseInt(NumBreuvage.getText()); // calculate total amount totales += selectedPizzaSize.getPrice() * numPizza; totales += selectedToppingSize.getPrice() * numTopping; totales += selectedBreuvage.getPrice() * numBreuvage; double historytotal = (numPizza * selectedPizzaSize.getPrice()) + (numTopping * selectedToppingSize.getPrice() + (numBreuvage * selectedBreuvage.getPrice())); String historyEntry = numPizza + " Pizza de taille " + selectedPizzaSize + " avec " + numTopping + " " + selectedToppingSize + " \ngarniture - Cout: " + historytotal; historyList.add(historyEntry); historyMap.put(historyEntry, historytotal); // clear text fields NumPizza.setText("0"); NumTopping.setText("0"); NumBreuvage.setText("0"); } }); Ajouter.setBounds(234, 552, 160, 50); contentPane.add(Ajouter); JButton Quitter = new JButton("Quitter"); Quitter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); Quitter.setBounds(33, 552, 160, 50); contentPane.add(Quitter); JButton Payer = new JButton("Payer"); Payer.setBounds(431, 552, 160, 50); contentPane.add(Payer); Payer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Receipt receiptWindow = new Receipt(totales, historyList); receiptWindow.setVisible(true); setVisible(false); } }); } - Assistant
It appears the main issue causing the ArrayIndexOutOfBoundsException is in the Receipt class, when attempting to split the historyEntry string. The input here should not have an issue, but to avoid any potential array index out of bounds exception, you can add a check before accessing
splitEntry[1]as follows:for (String historyEntry : historyList) { String[] splitEntry = historyEntry.split(" - Cout: "); if (splitEntry.length > 1) { previousHistory.put(splitEntry[0], Double.valueOf(splitEntry[1])); } }Now, the code should run without throwing an ArrayIndexOutOfBoundsException.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.