Prevent NPE by more checks

Should not be really a problem after
db ref. integrity improvement (FK)
Merge remote-tracking branch 'github/master' into research