1

When I tried to populate my choiceBox with an array of strings in the start method, I got a NullPointerException. I tried to use methods found online but nothing is working

        @FXML
        private ChoiceBox<String> enterState; 
        @FXML
        private ChoiceBox<Integer> enterYearJoined;
        @FXML
        private CheckBox enterIsHeActive;
        @FXML
        private Button enterStudentInfo;

        /*What the ChoiceBox needs to contain
         * 
         * "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA",
    "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH",
    "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX",
    "UT", "VT", "VA", "WA", "WV", "WI", "WY"*/
        public void start(Stage primaryStage) {
            try {

                Parent root = FXMLLoader.load(getClass().getResource("\\CreateNewStudent.fxml"));
                Scene scene = new Scene(root,500,300);
                primaryStage.setScene(scene);
                primaryStage.show();
                enterState.setItems(FXCollections.observableArrayList("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA",
                "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH",
                "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX",
                "UT", "VT", "VA", "WA", "WV", "WI", "WY"));

            } catch(Exception e) {
                System.out.println("ERROR IN MAIN");
                e.printStackTrace();

            }
        }

It gives me an error as soon as I reach the enterState method. Did I make a stupid mistake? Did I not put this in the right place? help

Rahul Shah
  • 141
  • 2
  • 12
  • `enterState` is most likely `null`. Is it associated with a value from the FXML? – MadProgrammer Oct 22 '15 at 05:37
  • I do not see `enterState` annotated with `@FXML` – ItachiUchiha Oct 22 '15 at 05:44
  • Oh sorry yeah it does have @fxml – Rahul Shah Oct 22 '15 at 23:26
  • Making your Application class a controller is usually a ["bad idea"](http://stackoverflow.com/questions/32081713/javafx-controller-class-not-working), unless you really know what you are doing, the application is probably not behaving as you think it is. Just make the controller and the application separate classes and things will be much easier to reason about with less chance of errors. If, after doing that, you continue to have issues, provide an [mcve](http://stackoverflow.com/help/mcve) and full stack trace. – jewelsea Oct 22 '15 at 23:32
  • Then what is the difference between a view class and a view controller class? – Rahul Shah Oct 23 '15 at 00:21
  • @RahulShah In the JavaFX framework, following from the MVC paradigm, the view class is your FXML specification. It specifies where the framework is to place all the widgets, in the proper location, and the classes that provide the node implementations all handle the task of taking your model data and updating the view objects. The controller class is what interacts with the model data and responds to user input events. – Ryan J Oct 23 '15 at 00:33
  • So, when i work on my project, i should keep all fxml files in a separate package, and my MainApp in a view controller package? Would I need any .java files in the view package at all? – Rahul Shah Oct 23 '15 at 00:51

0 Answers0