Usage of @Transient annotated field

Very new to Cuba, apologies if this is a simple issue.

I have a very simple invoice / invoice line composition that worked fine until I added the following transient linePrice property.


    @Column(name = "QUANTITY", nullable = false)
    protected Integer quantity;

    @Column(name = "UNIT_PRICE", nullable = false)
    protected BigDecimal unitPrice;

    @Transient
    @MetaProperty(related = {"quantity", "unitPrice"})
    protected BigDecimal linePrice;

    public BigDecimal getLinePrice() {
        return getUnitPrice().multiply(BigDecimal.valueOf(getQuantity()));
    }

I added linePrice to my view but whenever I try to edit I get a NullPointerException pointing to the “return” line in getLinePrice.

Any pointers appreciated.

Probably it is because the fields are null when the object is just created in memory. Try to initialize the fields:

@Column(name = "QUANTITY", nullable = false)
protected Integer quantity = 0;

@Column(name = "UNIT_PRICE", nullable = false)
protected BigDecimal unitPrice = BigDecimal.ZERO;

@Transient
@MetaProperty(related = {"quantity", "unitPrice"})
protected BigDecimal linePrice;

public BigDecimal getLinePrice() {
    return getUnitPrice().multiply(BigDecimal.valueOf(getQuantity()));
}