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

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -