Eladian
Eladian

Reputation: 958

JavaFx NullPointer Exception when populating a ListView

I have only been working with JavaFX for the past day but seem to be experiencing a null pointer exception when trying to populate a list view.

Basically I have login form and when the user logs in a new .fxml form is loaded which contains an accordion with a list view inside.

I would like the list view to populate once the user clicks the login button. debugging show that AccountMenu is null, however I cannot figure out why. Can anyone point me in the right direction?

Thanks

public class Controller {
    @FXML
    ListView<String> AccountMenu;


 void Login(ActionEvent event) {
   try {
            ObservableList<String> items =FXCollections.observableArrayList (
                    "Item one","Item two");
            AccountMenu.setItems(items);

        }catch(Exception e){
            e.printStackTrace();
        }
}

Part of the fxml file:

<Accordion layoutY="136.0" prefHeight="768.0" prefWidth="210.0">
    <panes>
        <TitledPane id="MyAccount" animated="false" text="My account">
          <content>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                 <children>
                    <ListView id="AccountMenu" fx:id="AccountMenu" layoutX="-1.0" prefHeight="658.0" prefWidth="210.0" />
                 </children>
              </AnchorPane>
          </content>
        </TitledPane>
      <TitledPane animated="false" style="-fx-background-color: red;" text="Books">

Upvotes: 0

Views: 2374

Answers (1)

user3679868
user3679868

Reputation: 693

First you should follow the java coding style, attributes and variables should start with a lower case.

Your FXML does not show if you declared a controller, your top container should have the xml attribute fx:controller="<your package>.Controller", and you should access your controller through the method FXMLLoader.getController() otherwise no injection occurs.

Upvotes: 1

Related Questions