fix #3708: NPE in database cleaning

This happens only when the database is in bad shape, but prevents from
seeing the error message.