0

El problema lo tengo en el 'case 1' que la variable 'ListaNotas' la cual me marca el error " The local variable ListaNotas may not have been initialized "y no se como solucionarlo.

    int salir = 0;
    int cont = 0;

    do {

        String[] opciones = { "Crear anotación", "Listar anotaciones", "Buscar", "Modificar", "Eliminar",
                "Marcar como importante", "Desmacar como importante", "Listar importantes", "Salir" };
        int opcion = JOptionPane.showOptionDialog(null, "Elige una de las opciones", "ANOTACIONES",
                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, opciones, null);

        switch (opcion) {
        case 0:
            String ListaNotas[] = new String[100];
            String NotaIntroducida = JOptionPane.showInputDialog(null, "Crear nueva nota", "Notas",
                    JOptionPane.INFORMATION_MESSAGE);

            List Lista = new ArrayList();
            Lista.add(NotaIntroducida);
            Lista = Arrays.asList(ListaNotas);

            break;

        case 1:
            
            for (int i = 0; i < ListaNotas.length; i++)
                JOptionPane.showMessageDialog(null, (ListaNotas[i].toString()));

            break;
2
  • 1
    Estás creando ListaNotas en el case 0, por tanto, no sería reconocida en los otros case. Para evitar esto debes crear la variable antes de empezar el bloque switch. O sea, poner esto: String ListaNotas[] = new String[100]; justo antes de switch (opcion) {. Aunque no le veo mucho sentido a tu código, por ejemplo, no veo que llenes esa variable en ninguna parte ¿?
    – A. Cedano
    el 3 nov. 2021 a las 20:16
  • Revisa el ámbito de las variables creadas dentro de un case; otro error que puedo notar es que tu variable Lista está cumpliendo la función de ListaNotas.
    – Joshin
    el 3 nov. 2021 a las 20:39

1 respuesta 1

0

Según el error, seria porque la variable ListaNotas del case 0 no estaría inicializada, en el case 1 la inicializaste como new String, si la idea es usar esa variable en todas las opciones del Switch, tendrías que inicializarla fuera de este.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.