7 Replies Latest reply: Oct 31, 2012 2:02 PM by 971758 RSS

    Trouble with Arrays of Menus and Menu Items

    971758
      Okay the short story is I am designing a GUI for an application, it has a lot of menus, and a lot of menu items per menu.
      I thought like in normal Java I could simply make an array of Menus and several arrays of menu items. My problem is that it compiles fine, but spews tons of exceptions. I am using Netbeans as my IDE.

      Here is a sample of my code. Its not the whole thing, but its the part that I suspect has problems since it worked before I switched to doing them as arrays.




      Menu [] menus = new Menu [11];
      menus[0].setText("File");
      MenuItem [] file = new MenuItem [8]; //FILE MENU DEFINITIONS


      file[2].setText("Open"); //open
      file[2].setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent arg0) {

      FileChooser fc = new FileChooser();

      File file =fc.showOpenDialog(primaryStage);

      }
      });

      // file[2].setAccelerator(null);



      menus[0].getItems().addAll(file);

      MenuBar menubar = MenuBarBuilder.create()

      .build();




      menubar.getMenus().addAll(menus);


      VBox vbox1 = VBoxBuilder.create()
      .children(menubar,scrollPane1,btn)
      .build();


      ap.getChildren().addAll(vbox1);


      Anyways the stuff compiles just fine no errors. But Netbeans spews exceptions like a guy who chugged a bottle of ipecac and the programs bombs.
      Here is some of them.

      Exception in Application start method
      java.lang.reflect.InvocationTargetException
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
           at java.lang.reflect.Method.invoke(Method.java:601)
           at com.javafx.main.Main.launchApp(Main.java:642)
           at com.javafx.main.Main.main(Main.java:805)
      Caused by: java.lang.RuntimeException: Exception in Application start method
           at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
           at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
           at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
           at java.lang.Thread.run(Thread.java:722)
      Caused by: java.lang.NullPointerException
           at javafxapplication2.JavaFXApplication2.start(JavaFXApplication2.java:37)
           at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
           at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
           at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
           at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
           at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
           at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
           ... 1 more
      Java Result: 1

      I am not really sure what to do. I would like some recommendations.
      If possible I would still like to use arrays of Menus and Menu Items, but if its not possible, I would like to know and understand why not.
        • 1. Re: Trouble with Arrays of Menus and Menu Items
          David Grieve
          jfxrt.jar is not on your class path.
          • 2. Re: Trouble with Arrays of Menus and Menu Items
            971758
            I apologize if I don't fully understand. I am still kind new to this. But It was my understanding that .jar file was made after compiling not before. I am not really sure what you mean by class path. Could you give an example. Again this code worked wonderfully before I used arrays. If I comment it out and replace it with dummy code like this it compiles and runs just fine.



            MenuItem open = new MenuItem("TEST");
            Menu debug =new Menu("DEBUG");
            debug.getItems().add(open);

            MenuBar menubar = MenuBarBuilder.create()

            .build();




            menubar.getMenus().addAll(debug);

            StackPane rootz = new StackPane();

            final TextArea textArea =TextAreaBuilder.create()
            .prefWidth(600)
            .prefHeight(400)
            .wrapText(true)
            .build();

            ScrollPane scrollPane1 = new ScrollPane();
            scrollPane1.getStyleClass().add("no-border-scroll-pane");

            scrollPane1.setMinSize(400, 400);




            scrollPane1.setContent(textArea);

            VBox vbox1 = VBoxBuilder.create()
            .children(menubar,scrollPane1,btn)
            .build();


            ap.getChildren().addAll(vbox1);











            btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
            System.out.println("Hello World!");
            FileChooser fc = new FileChooser();

            File file =fc.showOpenDialog(primaryStage);

            }
            });


            Scene scene = new Scene(ap, 760, 500);

            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
            }

            /**
            * The main() method is ignored in correctly deployed JavaFX application.
            * main() serves only as fallback in case the application can not be
            * launched through deployment artifacts, e.g., in IDEs with limited FX
            * support. NetBeans ignores main().
            *
            * @param args the command line arguments
            */
            public static void main(String[] args) {
            launch(args);
            }
            }
            • 3. Re: Trouble with Arrays of Menus and Menu Items
              David Grieve
              I guess I should have looked more closely at the exception. What is on line 37 of JavaFXApplication2? Something there is null.

              Caused by: java.lang.NullPointerException
              at javafxapplication2.JavaFXApplication2.start(JavaFXApplication2.java:37)
              • 4. Re: Trouble with Arrays of Menus and Menu Items
                971758
                Okay, at line 37 I have
                menus[0].setText("File");
                • 5. Re: Trouble with Arrays of Menus and Menu Items
                  971758
                  Oh..... I think I know what is wrong...
                  Menu[n] never had the regular menu constructor called and therefor can't really be initialized...

                  instead of menus[0].setText("File");


                  What I bet I need is

                  menus[0]= new Menu("File");
                  • 6. Re: Trouble with Arrays of Menus and Menu Items
                    James_D
                    (Sorry, this cross-posted with your previous message.)

                    You have a NullPointerException because you're trying to reference menus[0] when it hasn't been initialized:
                    Menu[] menus = new Menu[11] ;
                    creates a reference to an array of 11 Menus, but doesn't initialize them. So each element of the array is null.
                    menus[0].setText("File");
                    throws a NullPointerException as you're trying to call a method via an object reference (menus[0]) that is null.

                    You need
                    Menu[] menus = new Menu[11];
                    menus[0]=new Menu();
                    menus[0].setText("File");
                    // or just replace those two lines with menus[0]=new Menu("File");
                    and similarly, presumably, for all the other elements of the array menus.

                    You have similar problems for the array(s) of menu items.

                    Edited by: James_D on Oct 31, 2012 12:00 PM
                    • 7. Re: Trouble with Arrays of Menus and Menu Items
                      971758
                      Thank you everyone. Yep that was the problem. I was trying to call a method of an uninitialized element of an array of objects.