Error creating OrmLite database on Android 5.1.1 and above

387 views
Skip to first unread message

Oleg Ignatiev

unread,
Nov 10, 2015, 2:18:59 PM11/10/15
to ORMLite Developers

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.

Oleg Ignatiev

unread,
Nov 12, 2015, 11:06:56 AM11/12/15
to ORMLite Developers
I've realised that the problem is in foreign field named offer.
When I write 
@DatabaseField(foreign=true, foreignAutoRefresh = true, foreignAutoCreate=true)
instead of
@DatabaseField(foreign=true, foreignAutoRefresh = true, foreignAutoCreate=true, foreignColumnName = FIELD_ID)

I've got no error, but that is not what I want.

Here's the code of Offer class

package com.stroycapital.tkaerobus.model;
import java.util.Collection;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;
import com.stroycapital.tkaerobus.utils.db.DbHelper;

@DatabaseTable(tableName= DbHelper.TABLE_OFFER)
@JsonIgnoreProperties(ignoreUnknown=true)
public class Offer extends BaseModel 
{
    private static final long serialVersionUID = 2462329522062967596L;
public static final String TYPE_PURCHASE = "purchase",
KEY_TYPE = "type";
    public static final String IS_FAVOURITE="is_favorite";
    public static final String OFFER_ID_PARAM="id_offer";
@DatabaseField(id=true, unique=true, canBeNull=false, columnName=FIELD_ID)
@JsonProperty(required=true)
public int id;
@DatabaseField(canBeNull=false)
public String name;
@DatabaseField(canBeNull=false)
public String description;
@DatabaseField(canBeNull=false)
public String type;
@DatabaseField
public String main_image;
@DatabaseField
public String sex;

@DatabaseField
public int age_from;

@DatabaseField
public int age_to;

@DatabaseField
public String for_pregnant;

@DatabaseField
public String share_url;

@DatabaseField(canBeNull=false)a
public String cost;
@DatabaseField(canBeNull=false)
public boolean is_deleted;
@DatabaseField(canBeNull=false)
public boolean is_active;
@DatabaseField(canBeNull=false)
public boolean is_favorite;

@DatabaseField
public int _order;
@ForeignCollectionField(eager=true, columnName=FIELD_ID)
public Collection<OfferImage> images;
@ForeignCollectionField(eager=true, columnName=FIELD_ID, foreignFieldName = "offer")
public Collection<OfferCategory> category_ids;
@DatabaseField(canBeNull = false, foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
public Partner partner_id;
@DatabaseField(dataType = DataType.SERIALIZABLE)
    public String[] featured;
public Offer() 
{
super();
}
}

вторник, 10 ноября 2015 г., 22:18:59 UTC+3 пользователь Oleg Ignatiev написал:

Kevin Galligan

unread,
Nov 12, 2015, 11:50:04 AM11/12/15
to Ormlite Dev
To clarify, you want to specify '_id' on the other table, but that's
the name of the id column anyway, so I'm not sure what you'd
accomplish.

Ideally we could get a simple example demonstrating the issue and work
from there. Difficult to diagnose without a repro.
> --
> You received this message because you are subscribed to the Google Groups
> "ORMLite Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ormlite-dev...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Kevin Galligan
http://about.me/kpgalligan
Reply all
Reply to author
Forward
0 new messages