K
K
Konstantin Malyarov2016-05-19 13:31:02
Java
Konstantin Malyarov, 2016-05-19 13:31:02

How to add ItemMenu array to JavaFX MenuButton?

We read from the file, translate the string into an array of words. Give each itemMenu a name, add an itemMenu array to the menuButton...

@Override
    public void start(Stage primaryStage) throws Exception{
        List<String> items = null;
        List<MenuItem> items1 = null;
        try {
            File file = new File("./subgroup.xml");
            FileReader reader = new FileReader(file);
            BufferedReader in = new BufferedReader(reader);
            String string;
            while ((string = in.readLine()) != null) {
                int lenght = string.length();
                string = string.substring(1, lenght-1);
                items = Arrays.asList(string.split("\\s*,\\s*"));
            }
            in.close();
            int leghtArray = items.size() - 1;
            while (leghtArray > -1){
                MenuItem menuItem = new MenuItem();
                menuItem.setText(items.get(leghtArray));
                leghtArray--;
                items1.add(menuItem);
            }
            idGroup.getItems().addAll(items1);
        } catch (IOException e) {
            e.printStackTrace();
        }

An error is thrown...
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
  at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
  at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
  at sample.Main.start(Main.java:44)
  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
  at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
  at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
  at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
  ... 1 more

This is line 44!
items1.add(menuItem);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vayho, 2016-05-19
@vayho

You have items1 = null, fix it like this:
List<MenuItem> items1 = new ArrayList<>();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question