• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException on property binding

 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning Java and JavaFX. I am attempting to implement a card game as my excuse for a first project. I have implemented most of my model classes and am now working on the GUI. I have been using SceneBuilder to create the UI (in FXML).

I figure, to keep the model in sync with the view, I'll need to bind some properties from my model to my view. However, this is where I am having difficulty.

A bit of explanation, I have paced "Card" objects into an FXCollections.observableArrayList. The cards have an enumerated property which specifies what type of card it is (it's not a standard deck of playing cards). I would like to bind each element in the observableArrayList to a respective textProperty of a label in the view. Therefore, whenever the ArrayList is modified, a card is added or removed from the player's hand, the view is updated. Summary code is as follows:


game = reference to a model class
getPlayerHand() = returns a reference to an observableArrayList
get(0) = returns the first element in the ArrayList (an object of type Card)
getType() = returns the enumerated value of the CardType (e.g. CardType.GO, CardType.STOP, CardType.ACCIDENT)
nameProperty() = returns the String representation of the name property (e.g. "Go", "Stop", "Accident")

However, when I run the application, it gives me a NullPointerException on the first binding statement (line #19) in displayHand(). It acts as though it doesn't know what card0Label is. I have assigned "card0label" as the "fx:id" in the respective Label and also have verified that the controller is also set in the FXML. Have I done something wrong? If I comment out the binding statements the application runs without error. Also If I instantiate the label it will also run without the NPE.



What am I doing wrong? I can't seem to figure it out...all my efforts have been in vain. I'm in need of another pair of eyes on this one. Will you help? I have this project posted on GitHub if it helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic