Shield from NPE and invalid charEncoding