Java – ORMLite : Internal DAO object is null

ORMLite : Internal DAO object is null… here is a solution to the problem.

ORMLite : Internal DAO object is null

I’m using ORMLite, trying to use ForeignCollectionKey but I get the following error:

Internal DAO object is null. LazyCollections cannot be used if they have been deserialized.

I have an object called Zone:

public class Zone implements Serializable {

private static final long serialVersionUID = 1L;
    public static final String ZONE_ID = "id"; 
    public static final String ZONE_PARENT_ID = "parentZoneId";

    private int id;
    String name;
    @DatabaseField(foreign=true, foreignAutoRefresh = true)
    Zone parentZone;

@ForeignCollectionField(foreignFieldName = "parentZone", eager = true)
    private ForeignCollection<Zone> zoneChild;

public Zone() {
         TODO Auto-generated constructor stub
    public ForeignCollection<Zone> getZoneChild() {
        return zoneChild;
    public void setZoneChild(ForeignCollection<Zone> zoneChild) {
        this.zoneChild = zoneChild;
    public String getName() {
        return name;
    public void setName(String name) { = name;

In a class, I’m executing a recursive method to get all my area child objects:

public void getZone(Zone zone, Dao<Zone, Integer> tempZoneDao){
    ZoneListEntity zoneEntity = new ZoneListEntity();

List<Zone> childList = new ArrayList<Zone>(zone.getZoneChild());
    set rootZone's children as ZoneListEntity
    for(Zone currentZone : childList){
        ZoneListEntity zoneGroup = new ZoneListEntity();
        System.out.println("Zone : "+currentZone.getName());
        getZone(currentZone, tempZoneDao);

When I first entered my getZone, everything went well. Then when I loop in getZone, the app crashes trying to access the subregion:

List<Zone> childList = new ArrayList<Zone>(zone.getZoneChild());

Do you have any ideas? Is my model constructed correctly?


Do you have any ideas ? Is my model construction right ? Thanks

So the exception message tries to explain what happened. I’m not sure how to improve it.

Internal DAO object is null. LazyCollections cannot be used if they have been deserialized.

You are trying to access zoneChild, which is a deserialized ForeignCollection. Because it has been deserialized, all underlying database configurations and connections cannot be reestablished. I guess this happens when it’s stored in Android Bundle? I’m not sure if this is the only case.

If you need to get the Zone child, you will have to call dao.refresh() on the entity after you to deserialize it by executing zoneDao or query it yourself.

Related Problems and Solutions