Error in java code read GSON: java.lang.NullPointerException

-1

I'm trying to read this file but when I run this error:

Exception in thread "main" java.lang.NullPointerException
    at principa.Main.main(Main.java:61)

but I do not know what is null

public class Main {

    public static void main(String[] args) {
        Reader json;
        Gson gson = new Gson();

        try {
            json = new FileReader("C:/Users/rodrigo/original.txt");
            Profissao[] profissao = gson.fromJson(json, Profissao[].class);

            System.out.println(profissao[0].getHabilidades().get(0).getName());
            System.out.println(profissao[0].getDescricao());

            //Escrever no json
            Type type = new TypeToken<List<Profissao>>(){}.getType();
            List<Profissao> profissoes = gson.fromJson(json, type); //converte o json para uma lista

            System.out.println(profissoes);
            Profissao profissaoNova = new Profissao(); //cria uma nova profissao

            Local local = new Local();
            Habilidades hab = new Habilidades();

            profissaoNova.setDescricao("Encanador");

            local.setEndereco("Rua buquirinha 2");
            local.setTitle("Buquirinha 2");
            local.setlatitude(-23.2556945);
            local.setLongitude(-45.6995612);

            profissaoNova.getLocal().add(local);
            profissaoNova.getLocal().add(local);

            hab.setId(0);
            hab.setName("feliz");
            profissaoNova.getHabilidades().add(hab);

            System.out.println(profissaoNova.getHabilidades().get(0).getName());

            profissoes.add(profissaoNova); //adiciona a profissao no array

            String jsonString = gson.toJson(profissoes); //transforma para json novamente

            System.out.println(jsonString);

            Object[] Aprofissoes = profissoes.toArray();

            FileWriter writer;
            try {
                writer = new FileWriter("C:/Users/rodrigo/teste.txt");
                writer.write(jsonString);
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}

Regarding my JSON is this one

[
  {
    "descricao": "Professor",
    "local": [
      {
        "latitude": -23.1843473,
        "longitude": -45.8840718,
        "title": "Microcamp",
        "endereco": "rua vilaça 2010"
      },
      {
        "latitude": -23.1843473,
        "longitude": -45.8840718,
        "title": "Microcamp",
        "endereco": "rua vilaça 2010"
      }
    ],
    "habilidades":[
        {
          "id":0,
          "name":"fotografo"
        },
        {
          "id":1,
          "name":"escritor"
        }
      ]
  }
]
    
asked by anonymous 04.12.2017 / 02:32

1 answer

2

As you have not stated which line 61 is the same, and also did not initialize the collection of Local , I will assume line 61 is where the first line of this type is below. The solution is to initialize the collection.

profissaoNova.getLocal().add(local);

In addition, where you are converting json to a list, you are having computational cost with all the processing needed to convert JSON again. Ideally, you should simply use the previous conversion, as below:

List<Profissao> profissoes = Arrays.asList(profissao);
    
04.12.2017 / 13:03