I'm using OrmLite library to work with database in my Android application. It works fine on all versions of Android, but I faced with a trouble on Android 5.1.1 (API 22) and above. I have a class DBHelper extends OrmLiteSqliteOpenHelper
. In the onCreate method I create tables for my database
@Override
public void onCreate(SQLiteDatabase db, final ConnectionSource connectionSource)
{
try {
for (Class c : models)
TableUtils.createTableIfNotExists(connectionSource, c);
}
catch (Throwable e) {
e.printStackTrace();
Logger.e("error creating DB " + DATABASE_NAME);
}
}
Variable models
is an array of classes that describing fields of each table.
Class[] models = {
User.class,
Profile.class,
Offer.class,
Partner.class,
OfferCategory.class,
PrizeCategory.class,
...
I found that when table OfferCategory is created there is an exceptionjava.lang.NullPointerException: Attempt to invoke interface method 'int com.j256.ormlite.field.DataPersister.getDefaultWidth()' on a null object reference
. Here is my class OfferCategory
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
@JsonIgnoreProperties(ignoreUnknown=true)
@DatabaseTable(tableName= DbHelper.TABLE_CATEGORY)
public class OfferCategory implements Serializable
{
public static final String FIELD_ID = "_id",
FIELD_ORDER = "_order";
public Bundle toBundle()
{
final Bundle b = new Bundle();
b.putSerializable(getClass().getName(), this);
return b;
}
public static final BaseModel fromBundle(final Bundle b, final Class<?> cl)
{
if (b != null && b.containsKey(cl.getName()))
{
return (BaseModel) b.getSerializable(cl.getName());
}
return null;
}
/**
*
*/
private static final long serialVersionUID = 7805031064669698244L;
@DatabaseField(unique=true, canBeNull=false, columnName=FIELD_ID, id=true)
@JsonProperty(required=true)
public int id;
@DatabaseField
public String name;
@DatabaseField(foreign=true, foreignAutoRefresh = true, foreignAutoCreate=true, foreignColumnName = FIELD_ID)
public Offer offer;
public OfferCategory()
{
}
public OfferCategory(final int id)
{
this.id = id;
}
}
I have no idea, why this code works fine on all Android versions, but throws exception on Android 5.1.1 (API 22) and above. On more recent Android versions all works fine.
Please tell me what could be the problem? Thanks.