Null pointer exception when creating custom Dao with ormlite-android

I am trying to extend the ORMLite DAO base class to add some custom methods. I tried to answer this question here, but I am getting a null error and not sure how to draw the dao object correctly ( Ormlite - constructor call when no BaseDaoImpl extension is present ) I currently have the following table:

@DatabaseTable(tableName="beers", daoClass=BeerDao.class)
public class Beer {
    public static final String BEER_NAME = "name";

    @DatabaseField(generatedId = true)
    private UUID id = UUID.randomUUID();

    @DatabaseField()
    private String name;

    @DatabaseField()
    private String breweryName;

    public Beer() {}

    ... getters/setters
    }

      

BeerDao class:

public class BeerDao<Beer,UUID> extends BaseDaoImpl<Beer,UUID> {

    public BeerDao(ConnectionSource connectionSource, Class<Beer> dataClass) throws SQLException {
        super(connectionSource, dataClass);
    }
}

      

DatabaseHelper code:

public class DatabaseHelper extends OrmLiteSqliteOpenHelper  {
    private static final String TAG = "Database";

    private static final String DATABASE_NAME = "brewgenius.db";
    private static final int DATABASE_VERSION = 7;

    private BeerDao<Beer, UUID> beerDao = null;
    private Dao<Checkin, UUID> checkinDao = null;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
    }

    /* ... onCreate and onUpgrade code ..*/

    /**
     * Get Beer Model DAO
     * 
     * @return Beer DAO
     */
    public BeerDao<Beer, UUID> getBeerDao() {
        if (beerDao == null) {
            try {
                beerDao = getDao(Beer.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return beerDao;
    }

      

}

In my activity, when I try to get Tao, I get a null pointer exception.

BeerDao<Beer,UUID> dao = getHelper().getBeerDao();

      

FYI, Tao is transmitted:

BeerDao<Beer,UUID>

      

UPDATE

It looks like my DatabaseConfigUtil is not reading the DaoClass attribute.

public class DatabaseConfigUtil extends OrmLiteConfigUtil {
    public static void main(String[] args) throws SQLException, IOException {
        writeConfigFile("ormlite_config.txt");
    }
}

      

Removing the ormlite_config.txt from the DatabaseHelper constuctor, called on the fly, that reads the DaoClass attribute correctly. Any idea why the author isn't writing this?

Here ormlite_config.txt

# --table-start--
dataClass=com.brewgenius.model.Beer
tableName=beers
# --table-fields-start--
# --field-start--
fieldName=id
columnName=_id
generatedId=true
# --field-end--
# --field-start--
fieldName=name
# --field-end--
# --field-start--
fieldName=breweryName
# --field-end--
# --table-fields-end--
# --table-end--
#################################

      

+3


source to share





All Articles