0

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

Kalaiselvan
  • 1,997
  • 1
  • 14
  • 27
Eduardo Krakhecke
  • 295
  • 1
  • 6
  • 21
  • 1
    Then simply do a `entidades.setIdEntidade(json.get("pessoa").get("entidade") != null` first! And store if so that you can traverse from that safe point. – Antho Christen Dec 01 '17 at 11:55
  • @anchreg ok, but how to implemente that... and how about the date ? – Eduardo Krakhecke Dec 01 '17 at 12:09
  • `json.get("pessoa").get("entidade").get("idEntidade")== null` that is the basic problem I guess, because you dont check, if each parameter is not null, so, if pessoa or entidade will be null, you are not handling exception .. That sentece is ok, just in the case when `pessoa` and `entidade` will be available, in fact, you are checking just if `idEntidade` is present or not. Simply you have to add check for each parameter.. – xxxvodnikxxx Dec 01 '17 at 12:25
  • @ xxxvodnikxxx .. thats will be possible... but i dont have idea how i can do the new statment.. can you help me please ? – Eduardo Krakhecke Dec 01 '17 at 12:38
  • First way, the shortest one, but little messy is to putt whole sentence in the `try-catch` block, other way is to make over there per each parameter the `if` statement, I will put the answer post there Ah, no :D its locked because of kind of duplicate topic. – xxxvodnikxxx Dec 01 '17 at 13:00
  • eg. `entidades.setIdEntidade(json.get("pessoa").get("entidade").get("idEntidade")== null ? 0l: json.get("pessoa").get("entidade").get("idEntidade").asLong());` like this `if(json.get("pessoa") != null && json.get("pessoa").get("entidade") != null && json.get("pessoa").get("entidade").get("idEntidade") != null ){ entidades.setIdEntidade(json.get("pessoa").get("entidade").get("idEntidade")); }` – xxxvodnikxxx Dec 01 '17 at 13:06
  • Try to image this, that is exactly the same problem `String foo = null; int length = foo.length(); ` There is no problem with the length, but the whole foo object is null, so you cannot to call length on it – xxxvodnikxxx Dec 01 '17 at 13:10

0 Answers0