greenrobot/greenDAO · error · NullPointerException
Entity may not be null
Error message
Entity may not be null
What it means
getKeyVerified() guarantees a non-null key: if the passed entity itself is null, getKey(entity) is null and greenDAO throws NullPointerException with 'Entity may not be null'. This is a guard against passing a null entity into key-sensitive operations.
Solutions
- Null-check the entity before any DAO call.
- Filter nulls from collections before batch transactions.
- Return an Optional/null-safe result from the lookup that produced the entity.
- Throw early with a descriptive application-level message.
Example fix
// before
userDao.update(maybeUser); // maybeUser can be null
// after
if (maybeUser != null) {
userDao.update(maybeUser);
} Defensive patterns
Strategy: type-guard
Validate before calling
if (entity == null) return; // or throw an app-level IllegalArgumentException
Type guard
boolean isValidEntity(T e) { return e != null; } Try / catch
try {
T key = dao.getKeyVerified(entity);
} catch (NullPointerException e) {
if ("Entity may not be null".equals(e.getMessage())) {
// caller bug: null entity passed; log and fix call site
} else throw e;
} Prevention
- Null-check entities at API boundaries before DAO calls.
- Filter nulls from collections before batch transactions.
- Make lookups return Optional-style results instead of null entities.
- Fail fast in your own layer with descriptive messages.
When it happens
Trigger: Calling getKeyVerified(null), or internal update/delete paths that pass a null entity (e.g. updateInTx collections containing null).
Common situations: Collections built from optional lookups (map.get() returned null) then handed to insertInTx/updateInTx; cursors or joins producing null rows passed to the DAO.
Related errors
- Expected unique result, but count was
- Cannot delete entity, key is null
- Entity does not exist in the database anymore
- Cannot update entity without key - was it inserted before?
- ( ) does not have a single-column primary key
AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08).
Data as JSON: /api/errors/4f3dcf48c8c806dd.
Report an issue: GitHub.
Appendix: source
Thrown at DaoCore/src/main/java/org/greenrobot/greendao/AbstractDao.java:930
updateInTx(Arrays.asList(entities));
}
protected void assertSinglePk() {
if (config.pkColumns.length != 1) {
throw new DaoException(this + " (" + config.tablename + ") does not have a single-column primary key");
}
}
public long count() {
return statements.getCountStatement().simpleQueryForLong();
}
/** See {@link #getKey(Object)}, but guarantees that the returned key is never null (throws if null). */
protected K getKeyVerified(T entity) {
K key = getKey(entity);
if (key == null) {
if (entity == null) {
throw new NullPointerException("Entity may not be null");
} else {
throw new DaoException("Entity has no key");
}
} else {
return key;
}
}
/**
* The returned RxDao is a special DAO that let's you interact with Rx Observables without any Scheduler set
* for subscribeOn.
*
* @see #rx()
*/
@Experimental
public RxDao<T, K> rxPlain() {
if (rxDaoPlain == null) {
rxDaoPlain = new RxDao<>(this);View on GitHub (pinned to 0bbb338e17)