android - Realm.io, get NPE when trying to set list to a realm object -


i tried add list of realm objects one, hit npe

        realm.begintransaction();          // add person         person person = realm.createobject(person.class);         person.setid(1);         person.setname(name);          realmlist<cat> listofcats = new realmlist<cat>();         (int = 0; i<10; i++){             cat cat = new cat();             if(i!=5)                 cat.setname("cat "+i);             else                 cat.setname("tung");             listofcats.add(cat);         }         person.setcats(listofcats);          // when write transaction committed, changes synced disk.         realm.committransaction(); 

the line person.setcats triggers error:

       java.lang.nullpointerexception: attempt invoke interface method 'long io.realm.internal.row.getindex()' on null object reference 

if change code inside for loop:

       person.getcats().add(cat) 

instead of using separate realm list, works, i'm curious why first way doesn't, since writing code in second way seems cumbersome. thanks


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -