Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a Bean called Category. I need to include a SimpleListProperty in the bean to store children of the category.

I get an error that Children is null. Which is probably because it is not intialized.


How do I implement this?

What I have tried:

public class Category
{
    private final StringProperty categoryName = new SimpleStringProperty();
    private final IntegerProperty categoryId = new SimpleIntegerProperty();
    private final ListProperty<Topic> children = new SimpleListProperty<Topic>();

    public String getName() {
        return categoryName.get();
    }

    public void setName( String value ) {
        categoryName.set( value );
    }

    public StringProperty nameProperty() {
        return categoryName;
    }

    public int getId() {
        return categoryId.get();
    }

    public void setId( int value ) {
        categoryId.set( value );
    }

    public IntegerProperty idProperty() {
        return categoryId;
    }

    public List<Topic> childrenProperty() {
        return children;
    }

    @Override
    public String toString() {
        return categoryName.get();
    }

}
Posted
Comments
Richard MacCutchan 24-Aug-21 9:45am    
You are correct. You need to create the children object before you can pass it to a caller.
Mark F. 24-Aug-21 9:51am    
I read about Beans and I quote: "A no-arg constructor should be
there in a bean.".



Richard MacCutchan 24-Aug-21 9:54am    
Yes, but that has nothing to do with your problem. Your children object is a List of Topic objects, but as it stands you have not put any Topics into it.
Mark F. 24-Aug-21 10:00am    
I populated the list (children) in the controller class with test data. Each new child object was initialized and added to the Category.Children list. The compiler threw an exception. After some debugger stepping I found that the list was not initialized and was null.

I posted my fix but it shows as being moderated. Here is what I did:

public Category() {
ObservableList<topic> oList = FXCollections.observableArrayList( children );
this.children = new SimpleListProperty<topic>( oList );
}

Thanks for the help!
Richard MacCutchan 24-Aug-21 9:56am    
Try something simpler as documented at Getting Started with JavaFX[^]

I included a constructor and initialized the list.

public Category() {
    ObservableList<Topic> oList = FXCollections.observableArrayList( children );
    this.children = new SimpleListProperty<Topic>( oList );
}
 
Share this answer
 
v2
Adding a constructor and initializing the list as below fixed the issue.

public Category() {
    ObservableList<Topic> oList = FXCollections.observableArrayList( children );
    this.children = new SimpleListProperty<Topic>( oList );
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900