when I try to get a EntityDao, but java.lang.NullPointerException.

38 views
Skip to first unread message

潘鑫

unread,
May 3, 2016, 7:08:00 AM5/3/16
to greenDAO
public class ItemWriteServiceImpl extends BaseApplication implements ItemWriteService {

   
private ItemDao itemDao;

   
public ItemWriteServiceImpl() {
       
this.itemDao = getDaoSession().getItemDao();
   
}

   
@Override
    public Boolean create(Item item) {
       
try {
           
itemDao.insert(item);
       
}catch (Exception e) {
            e
.printStackTrace();
           
return Boolean.FALSE;
       
}
       
return Boolean.TRUE;
   
}
}

And the BaseApplication as the following shows:

public class BaseApplication extends Application {

   
public DaoSession daoSession;

   
public DaoMaster daoMaster;

   
public DaoMaster.DevOpenHelper openHelper;

   
public SQLiteDatabase database;

   
@Override
    public void onCreate() {
       
super.onCreate();
        setDatabase
();
   
}

   
private void setDatabase() {
        openHelper = new DaoMaster.DevOpenHelper(this, AppConstants.DB_NAME, null);
       
database = openHelper.getWritableDatabase();
        daoMaster = new DaoMaster(database);
       
daoSession = daoMaster.newSession();
   
}

   
public DaoSession getDaoSession() {
       
return this.daoSession;
   
}

   
public SQLiteDatabase getDatabase() {
       
return this.database;
   
}
}

And the question is why it (ItemDao) was NullPointerException ?


Reply all
Reply to author
Forward
0 new messages