質問

I have a JPA domain entity that I'm updating from user input. Depending on lots of factors about that object, it has actions to perform when it's updated (in this case, the update is to "mark it completed").

In the database, two "post-completion action configuration" fields (note and next_workflow) are nullable, and either have a value of interest, or are NULL. There may be very many of these, but I'm starting with these two.

I wrote the following method in the model class:

@PostUpdate
public void gotUpdate() {
    System.out.println("Got post update for " + this.getDescription());
    if (! this.getNote().isEmpty()) {
        Note n = new Note();
        n.setAssetId(this.getAssetId());
        n.setNotifyLessor(1);
        n.setNote(this.getLessorNote() + this.getCapturedData());
        n.setCreatedDate(new Date());
        n.persist();
    }
    System.out.println("In the middle of post update for " + this.getDescription());
    if (this.getNextWorkflow() != 0) {
        Asset a = this.getAssetId();
        a.setWorkflowId(Workflow.findWorkflow(this.getNextWorkflow()));
        a.merge();          
    }
    System.out.println("Finishing post update for " + this.getDescription());
}

For entities with NULL "note" values, the console output is:

Got post update for this item

For entities with non-NULL "note" values and with NULL "nextWorkflow" values, the console output is:

Got post update for this item
In the middle of post update for this item

No errors anywhere, no stack dump, no nothing. The method just silently quits, AND the merge I'm doing on this entity doesn't complete (the database remains unchanged).

Stepping through this in the debugger gets to that line where things are being tested, and if the value is NULL, it pops a tab saying "Source not found", which I don't really know what to make of. I think that's just the debugger saying it can't step into something, but I'm not actually asking it to...

役に立ちましたか?

解決

Rookie question, it turns out.

If Object's field field is null, then object.getField().length() is a call to a null pointer.

The answer is to test the field for nullness, not some side-effect of nullness.

if (! (this.getNote() == null)) {
...
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top