Reading and writing data between tables

I am new with cuba studio. I am trying to write code that read data from the database in a different table and set the data in another table using the middleware(service). Please do scan through the bellow code and explain to me where i went wrong. erro is: NullPointerException. Your help will be highly appreciated. Thank you

@Override
public void populateFixtures() {
    try (Transaction tx = persistence.createTransaction()) {
        TypedQuery<Teams> query = persistence.getEntityManager().createQuery("select e from transnamibfootballleague$Teams e", Teams.class);
        
        List<Teams> teamList = query.getResultList();
        
        Teams team = new Teams();
        
        Fixtures fixt = new Fixtures();
        
        for (int i = 0; i < teamList.size(); i++){
            fixt.setTeam1(team.getName());
            fixt.setTeam2(team.getName());
            persistence.getEntityManager().persist(fixt);
        }
        tx.commit();

Hi Indizoinamu,

you only create a empty new object

and use this always in your for-loop - that’s not working and creates this null-pointer-exception

remove the statement above and simply use for your loop

for (Team team : teamList) {
    fixt.setTeam1(team.getName());
    fixt.setTeam2(team.getName());
    persistence.getEntityManager().persist(fixt);
}

last thing - don’t use the new statement when creating a entity in CUBA - use instead:

Fixtures fixt = metadata.create(Fixtures.class);

To get access to metadata object just inject

@Inject
private MetaData metadata;

CU
Steven