Java code error read GSON: java.lang.Nullpointerexception

Asked

Viewed 111 times

-1

I’m trying to read this file but when I run it shows this error:

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

but I do not know what this 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();
        }   
    }
}

In relation to 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"
        }
      ]
  }
]

1 answer

2


Since you did not state which line 61 is, and also did not initialize the Local, I’m going to assume that line 61 is the one where the first line of this type is. The solution is to initialize Collection.

profissaoNova.getLocal().add(local);

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

List<Profissao> profissoes = Arrays.asList(profissao);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.