I have a hyerarchy of aggregation with data from a txt file. I'll try to draw it here
Documento has arraylist of Pagina
Pagina has arraylist of Parrafo
Parrafo has arraylist of Frase
Frase has arraylist of Palabra
Palabra has arraylist of basicElemento
To construct Documento, I only have to pass a string to the new Documento() sentence.
Constructor will split it and pass each piece to new Pagina(each piece),
Constructor of Pagina, will split the piece into new subpieces and create new Parrafo(subpiece) and so on.
So, constructors do all the job to build the whole structure calling methods each of teh sub-elements class. Note that this is not a inheritance hyerarchy. All the Pagina, Documento, Parrafo... are sons of the same super class.

I can't (or don't want to) create a Documento object, initialize it and later fill it with stuff. That is not what I wanted. I want the constructor to trigger the creation of all the subelements and the subelements of the subelements and so on, and the code is neat and short.
I tried to declare unDoc as null with

Documento unDoc = null;

but, eventhough later (in case 1 of switch #1) I really create and initialize it

unDoc = new Documento (unFichero.asString())

, what I got is that in case 1 of switch #2 (choice2), unDoc remained null.
The problem I have can be seen in this piece of code:

String titulo =   "              Menú Principal\n\n";
       String opciones = "1. Leer fichero de texto. \n\n"+
                         "2. Abrir descripción de fichero cargada. \n\n"+
                         "3. Guardar descripción de fichero. \n\n"+
                         "4. Salir del programa. \n\n";
       //Menu_consola: class that shows a textual menu in the terminal (dos-window)
       //has methods to change title and options.
       Menu_consola unMenu = new Menu_consola ( titulo, opciones);
       choice = unMenu.show();
       switch ( choice) {
        case 1: System.out.println (  "opción escogida: Leer fichero de texto"  );
                // user selected to read a file. 
                //create a leefichero object
                leeFichero unFichero = new leeFichero(); 
                // with unFichero we create a Documento
                Documento unDoc = new Documento(unFichero.asString());
                //closing the txt file
        case 2: //user selected to work with the statistics stored in unDoc
                //This option has no sense if unDoc has not yet been created
                unMenu.setTitulo("Menú Estadísticas\n\n");
                unMenu.setOpciones( "1. Mostrar estadísticas generales. \n\n"
                                   +"2. Mostrar estadisticas por página. \n\n"
                                   +"3. Mostrar estadísticas por párrafo. \n\n"
                                   +"4. Mostrar estadísticas por frase. \n\n"
                                   +"5. Mostrar elementos simples. \n\n"
                                   +"6. Menú Elementos. \n\n"
                                   +"7. Menú Principal. \n\n");
                int choice2 = unMenu.show();
                switch ( choice2) {
                  case 1: System.out.println("elección: estadísticas generales");
 // -v-v-v HERE GOES THE ERROR variable unDoc may have not been initialized -v-v-v->
                  case 2: System.out.println ( "elección: estadísticas por página");
                          // some activities here...
       //... rest of the switch cases here ...
6 Years
Discussion Span
Last Post by ankilosado

You are initializing unDoc in case 1 of switch #1. If the case 1 is true then, case 2 will be skipped. The same if the block inside case 2 is being implemented then case 1 must have been skipped.

I see that is probably the problem because switch statements act like 'else if' blocks, and keep checking till it finds a 'true' case. And when it finds one, it skips the rest.


THIS COMPILES!!!!! I have an amputee If Then statement, but this compiles!

public static void main (String [] args)
       int choice;
       do {
         String [] opciones1 = {"Leer fichero de texto.",
                             "Abrir descripción de fichero cargada.",
                             "Guardar descripción de fichero.",
                             "Salir del programa."};
         menu_consola unMenu=new menu_consola ("Menú Principal", opciones1);
         choice = unMenu.ejecutaMenu();
         Documento unDoc = null;
         switch ( choice) {
          case 1: System.out.println (  "opción escogida: Leer fichero de texto"  );
                   if (unDoc==null)
          case 2:   String [] opciones2 ={"Mostrar estadísticas generales.",
                                   "Mostrar estadisticas por página.",
                                   "Mostrar estadísticas por párrafo.",
                                   "Mostrar estadísticas por frase.",
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.