Reputation: 2203
I try to sum a column, but I get a null pointer exception and I don't know what I do wrong.
ChartVentaVendedores chart = new ChartVentaVendedores();
Criteria facturas = getSession().createCriteria(Factura.class);
facturas.add(Restrictions.like("estado", "CONFIRMADO"));
facturas.add(Restrictions.between("fechaCreacion", fromDate, toDate));
Criteria vendedorFactura = facturas.createCriteria("idVendedor");
vendedorFactura.add(Restrictions.eq("idUsuario", idVendedor));
facturas.setProjection(Projections.sum("total"));
Long counter = (Long) facturas.uniqueResult();
int value = counter.intValue();
chart.setValue(counter.intValue());
chart.setDate(fromDate);
Even if I didn't do the join, I still get the null
.
I tried to change the type of Long counter = (Long) facturas.uniqueResult();
to BigInteger
but I still get the same error.
My class:
@Entity
@Table(name = "factura")
public class Factura implements Serializable {
private int idFactura;
private Cliente cliente;
private String estado;
private Usuarios idVendedor;
private Date fechaCreacion;
private Date fechaModificacion;
private Integer usuarioCreacion;
private Integer usuarioModificacion;
private BigInteger total;
private String tipoFactura;
....
}
Upvotes: 2
Views: 2010
Reputation: 6946
I think, that your Projections.sum("total")
can't sum BigInteger
as it doesn't have autoboxing.
If you can, try change BigInteger
to Integer
or Long
.
Upvotes: 1
Reputation: 11
Just tried this to sum up ID of cities in a h2 db, sums up as required.
Criteria vendedorFactura = HibernateUtil.getSession().createCriteria(City.class);
vendedorFactura.add(Restrictions.eq("name", city.getName()));
vendedorFactura.setProjection(Projections.sum("id"));
Long counter = (Long) vendedorFactura.uniqueResult();
System.out.println("Counter:"+counter);
Counter:1161 ( Sum of 1 33 65 97 129 161 193 225 257)
Upvotes: 0