fix #1227 and other NPEs

fix #1227
fix #1221
fix #1222
fix #1223
fix #1224
fix #1225