Question

I'm using ormlite for android 4.47. All is good, but sometimes when run application I receive exception with next cause:

you must call initialize() before you can use the dao

In my Application class I'm init ormlite like write in are doc.

DatabaseFactory.setHelper(applicationContext);

What is going on ? This exception I receive not often. Maybe one in a day.

Full stack:

 java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.IllegalStateException: you must call initialize() before you can use the dao
            at com.j256.ormlite.dao.BaseDaoImpl.checkForInitialized(BaseDaoImpl.java:925)
            at com.j256.ormlite.dao.BaseDaoImpl.queryBuilder(BaseDaoImpl.java:247)
            at com.j256.ormlite.dao.BaseForeignCollection.getPreparedQuery(BaseForeignCollection.java:174)
            at com.j256.ormlite.dao.EagerForeignCollection.<init>(EagerForeignCollection.java:38)
            at com.j256.ormlite.field.FieldType.buildForeignCollection(FieldType.java:784)
            at com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:82)
            at com.j256.ormlite.stmt.SelectIterator.getCurrent(SelectIterator.java:270)
            at com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:161)
            at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:200)
            at com.j256.ormlite.stmt.StatementExecutor.queryForAll(StatementExecutor.java:118)
            at com.j256.ormlite.dao.BaseDaoImpl.queryForAll(BaseDaoImpl.java:239)
            at com.ls.dailysteals.core.database.dao.HeistDAO.getAllHeist(HeistDAO.java:31)
            at com.ls.dailysteals.ui.fragment.HeistFragment$1.doInBackground(HeistFragment.java:92)
            at com.ls.dailysteals.ui.fragment.HeistFragment$1.doInBackground(HeistFragment.java:88)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

Init:

    public class DSApplication extends Application {

        private static Context applicationContext;

        @Override
        public void onCreate() {
            super.onCreate();
            DatabaseFactory.setHelper(applicationContext);
    }
}

First call: Called in fragment lifecyrcle method onViewCreated(...);

databaseTask = new AsyncTask<Object, List<ShortDeal>, List<ShortDeal>>() {

            @Override
            protected List<ShortDeal> doInBackground(Object... params) {
                return DatabaseFactory.getHelper().getShortDealDAO().getAllShortDeal();
            }

            @Override
            protected void onPostExecute(List<ShortDeal> shortDeals) {
                super.onPostExecute(shortDeals);
                updateAdapter(shortDeals);
            }
        };

        if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            databaseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            databaseTask.execute();
        }
Was it helpful?

Solution 2

I've never used ORMLite, but mt guess this is because your applicationContext is always null, thus it thinks it's not initalized

public class DSApplication extends Application {

    private static Context applicationContext;

    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        DatabaseFactory.setHelper(this);
    }
    ....

OTHER TIPS

Try to use

YourDao dao = new YourDao(OrmLiteSqliteOpenHelper.getConnectionSource(), YourDao.class);

public class YourDao extends BaseDaoImpl<YourClass, Integer> {    
    protected YourDao(ConnectionSource connectionSource, Class<YourClass> dataClass) throws SQLException {
        super(connectionSource, dataClass);
    }
}

instead

Dao<YourClass, Integer> yourDao = OrmLiteSqliteOpenHelper.getDao(YourClass.class);

Before call YourDao.queryBuilder()

Constructor of BaseDaoImpl call initialize()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top