I am getting on my back end a json this way
"pessoa": {
"nome": "asas",
"tipoPessoa": "F",
"dataNascimento": "15/06/1983",
"foto": ""
},
"cns": "ewe",
"pessoasFisicas": {
"sexo": {
"idSexo": "1"
},
"estadoCivil": {
"idEstadoCivil": "2"
},
"dataObito": "11/12/2017"
},
"pessoasEnderecos": {
"cep": "966",
"pais": {
"idPais": "5"
},
"municipio": {
"idMunicipio": "1"
}
},
"pessoasContatos": {
"tipoContato": {
"idTipoContato": "1"
}
},
"pessoasCaracteristicas": {
"cbors": {
"idCbor": "1"
}
}
and I got two problems in my method to save it on the database. I have a null pointer exception in
Entidades entidades = new Entidades();
entidades.setIdEntidade(json.get("pessoa").get("entidade").get("idEntidade") == null ? 0l: json.get("pessoa").get("entidade").get("idEntidade").asLong());
but sure that method will be null because
the business rule to entidade
is not mandatory requirement
but I need to know how I do not get a nullPontException
in this case
look my method to save the data
@RequestMapping(method = RequestMethod.POST, value = "/pacientes")
public HttpStatus cadastrarPacientes(@RequestBody ObjectNode json) throws ParseException {
SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
Entidades entidades = new Entidades();
entidades.setIdEntidade(json.get("pessoa").get("entidade").get("idEntidade")== null ? 0l: json.get("pessoa").get("entidade").get("idEntidade").asLong());
Pessoas pessoas = new Pessoas();
pessoas.setNome(json.get("pessoa").get("nome").textValue());
pessoas.setNomeSocial(json.get("pessoa").get("nomeSocial")== null ? null : json.get("pessoa").get("nomeSocial").textValue());
pessoas.setFoto(json.get("pessoa").get("foto").textValue() == null ? null : json.get("pessoa").get("foto").textValue());
pessoas.setNomePai(json.get("pessoa").get("nomePai") == null ? null : json.get("pessoa").get("nomePai").textValue());
pessoas.setNomeMae(json.get("pessoa").get("nomeMae") == null ? null : json.get("pessoa").get("nomeMae").textValue());
pessoas.setDataNascimento(formato.parse(json.get("pessoa").get("dataNascimento").textValue()));
pessoas.setEntidade(new Entidades());
pessoas.setEntidade(entidades);
pessoas.setTipoPessoa(json.get("pessoa").get("tipoPessoa")== null ? null : json.get("pessoa").get("tipoPessoa").textValue());
Paises pais = new Paises();
pais.setIdPais(Long.parseLong(json.get("pessoasEnderecos").get("pais").get("idPais").textValue()));
PessoasFisicas pFisicas = new PessoasFisicas();
pFisicas.setDataCadastroPisPasep(formato.parse(json.get("pessoasFisicas").get("dataCadastroPisPasep")== null ? "" : json.get("pessoasFisicas").get("dataCadastroPisPasep").textValue()));
Ufs uf = new Ufs();
uf.setIdUf(json.get("pessoasEnderecos").get("uf").get("idUf")== null ? 0L : json.get("pessoasEnderecos").get("uf").get("idUf").asLong());
pacientesService.cadastrarPacientes(pFisicas, pCarac,pessoasEnderecos, pac );
return HttpStatus.CREATED;
}
i have a similar error in other case.. whe i pass a empty date i got a null too.. `
pFisicas.setDataCadastroPisPasep(formato.parse(json.get("pessoasFisicas").get("dataCadastroPisPasep") == null ? "" : json.get("pessoasFisicas").get("dataCadastroPisPasep").textValue()));
so i asking..how can I handle this error, remembering that by my business rule it is possible to have the date null and entidade
im using api Spring boot