2

Error appears in android project using ormlite I get compilation error on this code:

public class DatabaseModel {
        private Dao<Object, Integer> mDao = null;
        private DatabaseHelper mHelper;
        private Class<?> mClass;

        public DatabaseModel(DatabaseHelper h, Class<?> c) {
                mHelper = h;
                mClass = c;
                try {
                        mDao = mHelper.getDao(mClass);
                } catch (SQLException e) {
                        Debug.e("Can't get dao", e.getStackTrace());
                        throw new RuntimeException(e);
                }
        }

on line 25 mDao = mHelper.getDao(mClass);

Error: type parameters of <D>D cannot be determined; no unique maximal
instance exists for type variable D with upper bounds
     com.j256.ormlite.dao.Dao<java.lang.Object,java.lang.Integer>,
     com.j256.ormlite.dao.Dao<capture#296 of ?,?>

But when i tries to build project using eclipse it works fine

The error looks similar to this SO question.

I don't know whether this bug of Idea or javac.

My configuration: IntelliJ IDEA 11.0.2 Build #IC-111.277 Built on 1 Февраль 2012 г. JDK: 1.6.0_29 VM: Java HotSpot(TM) 64-Bit Server VM Vendor: Apple Inc.

Community
  • 1
  • 1
z0_0mer
  • 129
  • 3
  • 11

2 Answers2

1

I'm not getting this error in eclipse but I can see why you would see a problem. mdao is defined as Dao<Object, Integer> but you are calling getDao(mClass) where mclass is a Class<?>. Object != ? in generic land.

You could turn your entire class into a generic type. Something like the following would work.

public class DatabaseModel<T, ID> {
    private Dao<T, ID> mDao = null;
    private DatabaseHelper mHelper;
    private Class<T> mClass;

    public DatabaseModel(DatabaseHelper h, Class<T> c) {
        mHelper = h;
        mClass = c;
        try {
            mDao = mHelper.getDao(mClass);
        } catch (SQLException e) {
            Debug.e("Can't get dao", e.getStackTrace());
            throw new RuntimeException(e);
        }
    }
}

That should work.

Gray
  • 115,027
  • 24
  • 293
  • 354
0

In my case, I could circumvent the problem by using getDataDao() instead of getDao().

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187