Ebean throws Null Pointer Exception when int column is null
6-16-2017 update: Hmm, turns out this issue was partly due to my Java inexperience. Primitives can't be null. Oh well, I think this could still be relevant.
I had a model with an int column that could be nullable. However, when the value was actually null, Ebean threw a weird exception that wasn’t caught in the tests. Which was worrisome.
The model looked something like this:
@Entity
public class Foo extends Model {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(nullable=false, unique=true)
private long id;
@Column(nullable=true)
private int myOtherInt;
}
Here is the stacktrace:
Caused by: java.lang.RuntimeException: set numFspPoints on [models.Foo] arg[null] type[models.Foo] threw error
at com.avaje.ebeaninternal.server.deploy.BeanProperty.setValue(BeanProperty.java:693)
at com.avaje.ebeaninternal.server.query.SqlBeanLoad.load(SqlBeanLoad.java:76)
at com.avaje.ebeaninternal.server.deploy.BeanProperty.load(BeanProperty.java:580)
at com.avaje.ebeaninternal.server.query.SqlTreeNodeBean.load(SqlTreeNodeBean.java:270)
at com.avaje.ebeaninternal.server.query.CQuery.readNextBean(CQuery.java:435)
at com.avaje.ebeaninternal.server.query.CQuery.hasNext(CQuery.java:516)
at com.avaje.ebeaninternal.server.query.CQuery.readCollection(CQuery.java:547)
at com.avaje.ebeaninternal.server.query.CQueryEngine.findMany(CQueryEngine.java:330)
at com.avaje.ebeaninternal.server.query.DefaultOrmQueryEngine.findMany(DefaultOrmQueryEngine.java:105)
at com.avaje.ebeaninternal.server.core.OrmQueryRequest.findList(OrmQueryRequest.java:344)
Caused by: java.lang.NullPointerException: null
I found the solution here - basically, any nullable columns have to be changed from int
to Integer
, and I’m assuming long
to Long
.
I haven't tried to test it yet - I thought my tests already had that column nulled - but I'll update this if/when I try.
In the end, the model looked like this:
@Entity
public class Foo extends Model {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(nullable=false, unique=true)
private long id;
@Column(nullable=true)
private Integer myOtherInt; // <-- This is the line that changed
}