-1
private static final int Max_Platillos = 100;
private static Platillo[] platillos = new Platillo[Max_Platillos];

private static int numPlatillos = 0;
private static Platillo[] platillosIni = new Platillo[26];
private static int numeroPlatillosIni = 0;

static {
    String[] nombresPlatillos = {
        "Paella de Mariscos",
        "Ceviche de Pescado",
        "Sushi",
        "Tres Leches",
        "Caldereta de Langosta",
        "Fish and Chips",
        "Ostras Rockefeller",
        "Gambas al Ajillo",
        "Flan de Caramelo",
        "Pasta con Almejas",
        "Clam Chowder",
        "Tacos de Pescado",
        "Bacalao a la Vizcaína",
        "Salmón a la Plancha",
        "Tiramisú",
        "Langosta Thermidor",
        "Tiradito",
        "Moules Marinières",
        "Sopa de Mariscos",
        "Risotto de Mariscos",
        "Atún a la Tostada",
        "Cangrejo a la Chesapeake",
        "Calamares a la Romana",
        "Pulpo a la Gallega", //            "Coca Cola",
    //            "Té Frío"
    };

    String[] descripcionesPlatillos = {
        "Arroz, camarones, mejillones, almejas y langosta.",
        "Ensalada de pescado crudo marinado en jugo de limón.",
        "Arroz sazonado y una variedad de pescados y mariscos.",
        "Postre de Tres leches",
        "Un guiso de langosta típico de las Islas Baleares, España.",
        "Pescado rebozado con patatas fritas, un plato clásico británico.",
        "Ostras horneadas con una salsa de espinacas y pan rallado",
        "Gambas salteadas en aceite con ajo y guindilla",
        "Postre cremoso de caramelo y vainilla.",
        "Una pasta italiana clásica con almejas en una salsa de vino blanco.",
        "Una sopa cremosa de almejas, originaria de Nueva Inglaterra, Estados Unidos.\n",
        "Pescado a la parrilla o frito, servido en una tortilla de maíz",
        "Bacalao en una salsa de pimientos y tomates",
        "Salmón asado, comúnmente servido con verduras y limón.",
        "Postre italiano de capas de bizcocho y mascarpone.",
        "Langosta gratinada con una salsa cremosa",
        "Un plato de pescado crudo cortado en finas láminas y marinado con limón",
        "Mejillones cocinados al vapor con vino blanco",
        "Una sopa con una variedad de mariscos",
        "Un risotto italiano con camarones, mejillones, calamares y otros mariscos.",
        "Tostadas mexicanas con atún fresco, soja, aguacate y cebolla.",
        "Cangrejo azul sazonado y asado",
        "Anillas de calamar rebozadas y fritas",
        "Pulpo cocido y sazonado con pimentón", //            "Bebida de Coca Cola",
    //            "Bebida de Té Frío"
    };

    TipoPlatillo[] TipoPlato = {
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.SOPA,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.POSTRE,
        TipoPlatillo.SOPA,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.POSTRE,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.SOPA,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.POSTRE,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.SOPA,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.PLATILLO_PRINCIPAL,
        TipoPlatillo.BEBIDA,
        TipoPlatillo.BEBIDA
    };

    for (int i = 0; i < 25; i++) {
        String nombre = nombresPlatillos[i];
        String descripcion = descripcionesPlatillos[i];
        int precio = Math.round((float) ((Math.random() * (20000 - 10000 + 1)) + 10000));
        TipoPlatillo tipo = TipoPlato[i];

        platillos[i] = new Platillo(nombre, descripcion, precio, tipo);
        numPlatillos++;
    }
}

This part of the code generates the following error Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 24 out of bounds for length 24

But I have no idea since all arrays are defined to a quantity greater than that. And it is necessary that this method is initialized at the beginning to be able to create the initial dishes that go to the menu. I hope you can help me, thanks

I hope you can help me to solve this error, thanks

Can you solve my problem

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    [Don't use `static`](https://technojeeves.com/index.php/aliasjava1/108-static-is-a-swear-word) – g00se Aug 19 '23 at 20:50
  • 1
    Arrays are indexed starting at `0`. The array `nombresPlatillos` has 24 entries. What is the index of the last value? Compare this to the `for`-condition of the loop. – Turing85 Aug 19 '23 at 20:50
  • And this is one reason that you should not use "magic numbers" in your for loops (or anywhere else). Loop until the length of the array, no more and no less. Another unrelated suggestion: separate the *data* from the *code*. Why not create text files with the data, either plain text or JSON, and then read the data into the program when it starts? – Hovercraft Full Of Eels Aug 19 '23 at 20:53
  • @Turing85 What should I change in the code?, srry I'm very new in this world. – YerayCopley Aug 19 '23 at 21:01
  • @g00se And what should I replace it with? – YerayCopley Aug 19 '23 at 21:01
  • I will not provide a solution; this would rob you of the possibility to engage with the problem (and the information given) on your own, which will result in a better learning result. – Turing85 Aug 19 '23 at 21:03
  • Any basic Java tutorial that covers use of arrays and for loops will tell/show you how to fix this in the short term. In the long term, again, separate your data from your code. You can find great Java tutorials here: [The Java Tutorials](https://docs.oracle.com/javase/tutorial/). The [Language Basics Section](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html) will cover arrays and for loops well. – Hovercraft Full Of Eels Aug 19 '23 at 21:15
  • *And what should I replace it with?* If it's not the `static` in `public static void main` the answer is an empty string – g00se Aug 19 '23 at 21:16
  • For every static field you declare, that field will be "shared' by all instances of your class. Usually this is not something you want. – WJS Aug 19 '23 at 22:19

0 Answers0