1

Can someone explain me how to maintain data in array list during the running of app. Let say we have three classes. The main class run the app and the button "Save" add the data in the second class where is declared the private array list. Then when we added the data in array list , we click the button "Next scene". When is opened the second stage which is the third class, there is button "Show", that should show the data which were added in the first class, but it threw error that the array list is empty.

Code:

First class

 public void start(Stage primaryStage) {
       final Button btn = new Button();
            btn.setLayoutX(121);
        btn.setLayoutY(125);
        btn.setText("Save");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                array.insert(20);
            }
        });

        final Button btn_next = new Button();
        btn_next.setLayoutX(btn.getLayoutY());
        btn_next.setLayoutY(btn.getLayoutX() + 54);
        btn_next.setText("Next Scene");
        btn_next.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {


                secondStage.start();

            }
        });

        Pane root = new Pane();
        root.getChildren().add(btn);
        root.getChildren().add(btn_next);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Exercise");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

Second class

public class ArrayListClass {

    private ArrayList <Integer> array = new ArrayList<>();
    private ObservableList  name_obs = FXCollections.observableArrayList(array);



    public void insert(int maxBound) {

      for(int i = 0; i < maxBound; i++) {

          name_obs.add(i);
      }

    }


    public Integer show(int i) {


       return (Integer) name_obs.get(i);

    }

}

Third class

public class SecondStage {

    private Stage secondaryStage = new Stage();
    ArrayListClass array = new ArrayListClass();


    public void start() {


        Button btn= new Button();
        btn.setText("Show");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                System.out.println(array.show(5));
                }
            }
        );

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        secondaryStage.setTitle("Exercise");
        secondaryStage.setScene(scene);
        secondaryStage.show();
    }


}

ERROR:

Executing /Users/Jenda/NetBeansProjects/ExerciseArrayList/dist/run542796410/ExerciseArrayList.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:89)
    at exercisearraylist.ArrayListClass.show(ArrayListClass.java:36)
    at exercisearraylist.SecondStage$1.handle(SecondStage.java:36)
    at exercisearraylist.SecondStage$1.handle(SecondStage.java:31)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8216)
    at javafx.scene.control.Button.fire(Button.java:185)
user3770144
  • 105
  • 1
  • 2
  • 12
  • Can you show enough of the code for someone to be able to help? E.g. in your "First class", where are the variables `array` and `secondStage` declared and initialized? – James_D Jan 26 '15 at 19:37
  • You `SecondStage ` class creates a new instance of `ArrayListClass `. Are you sure that you are using the same instance in your first class? Otherwise, your array in `SecondStage ` will always be empty – Jan Gassen Jan 26 '15 at 19:51
  • Basically, at no point do you ever add anything to the `array` you create in your `SecondStage`. You only add data to the one you create in the "First class". I assume you want to share just one instance of `ArrayListClass` between the other two classes, but without the relevant code it's hard to give a fully useful answer. – James_D Jan 26 '15 at 19:51
  • Yes, that what I mean. Share data between two or more classes but I do not know how to reach it. – user3770144 Jan 26 '15 at 20:04
  • Or make the ArrayListClass as a static class, it could be the solution ? – user3770144 Jan 26 '15 at 20:09
  • Making it singleton will solve your immediate problem but might not lead to a nice clean design. Go ahead and make it singleton for now just to see if it works. But then look for a better way to share this object between those two classes. – Jose Martinez Jan 26 '15 at 20:10
  • @user3770144 Can you update your question to show the relevant code? Otherwise we are just guessing as to what those variables are. Making the class members static is a horrible idea. – James_D Jan 26 '15 at 21:15
  • see https://stackoverflow.com/a/45679685/1497139 for more links – Wolfgang Fahl Aug 14 '17 at 17:24

0 Answers0