jeudi 26 mars 2015

Custom DAO inheriting from BaseDaoImpl causes NullPointerException

I created my DaoEZTag class that extends BaseDaoImpl which allows to perform operations on the database.


But when I try to call this DAO's class in MyActivity this cause a NullPointerException in



daoTag = DatabaseManager.getInstance().getHelper().getDaoTag();


so in my DatabeHelper, getDaoTag() returns null but I do not understand why ? Thanks yo for your help !


My POJO class



@DatabaseTable(tableName = EZTag.TABLE_NAME, daoClass = EZTag.class)
public class EZTag
{
public static final String TABLE_NAME = "ez_tag";
public static final String COLUMN_TAG_ID = "tag_id";
public static final String COLUMN_PARENT_TAG_ID = "parent_tag_id";
public static final String COLUMN_TAG_KEYWORD = "keyword";

@DatabaseField(generatedId = true)
private int id;

@DatabaseField(columnName = EZTag.COLUMN_TAG_ID)
private int tagId;

@DatabaseField(columnName = EZTag.COLUMN_PARENT_TAG_ID)
private int parentTagId;

@DatabaseField(columnName = EZTag.COLUMN_TAG_KEYWORD)
private String keyword;

//Constructor, Getters & Setters

}


MyDatabaseHelper



public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

private static final String TAG = DatabaseHelper.class.getSimpleName();
private DaoEZTag daoTag;

/***************/

public DatabaseHelper(Context context) {
super(context, Config.Database.NAME, null, Config.Database.VERSION);
}


@Override
public void onCreate(SQLiteDatabase database,ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, EZTag.class);
} catch (SQLException e) {
Log.e(TAG, "Can't create database", e);
throw new RuntimeException(e);
} catch (java.sql.SQLException e) {
Log.e(TAG, "Can't create database", e);
}
}


@Override
public void onUpgrade(SQLiteDatabase db,ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, EZTag.class, true);
onCreate(db, connectionSource);
} catch (java.sql.SQLException e) {
Log.e(TAG, "Can't drop databases", e);
}
}


public DaoEZTag getDaoTag() {
if (null == daoTag) {
try {
daoTag = getDao(EZTag.class);
}catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return daoTag;
}

}


My DatabaseManager



public class DatabaseManager {

static private DatabaseManager instance;

static public void init(Context context) {
if(null==instance) {
instance = new DatabaseManager(context);
}
}

static public DatabaseManager getInstance() {
return instance;
}

private DatabaseHelper helper;

private DatabaseManager(Context context) {
helper = new DatabaseHelper(context);
}

public DatabaseHelper getHelper() {
return helper;
}

}


My custom DAO that extends BaseDaoImpl



public class DaoEZTag extends BaseDaoImpl<EZTag,Integer>
{
public DaoEZTag(ConnectionSource connectionSource) throws SQLException {
super(connectionSource, EZTag.class);
}

// Other methods for database operations
}


} And finally my Activity



public class MyActivity extends ActionBarActivity {

private static final String TAG = MyActivity .class.getSimpleName();
private DaoEZTag daoTag;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity)
DatabaseManager.init(this);
daoTag = DatabaseManager.getInstance().getHelper().getDaoTag();
Log.e(TAG,daoTag.toString());
}
}

Aucun commentaire:

Enregistrer un commentaire