I want to build a json object using Gson from a json file received as parameter while executing the jar.
This is the method I used to read the json file
public static JsonObject loadData(String filename) throws FileNotFoundException {
Gson parser = new Gson();
File jsonFile = new File(filename);
BufferedReader br = null;
JsonObject fileParsed;
try {
br = new BufferedReader(new FileReader(jsonFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fileParsed = parser.fromJson(br, JsonObject.class);
} catch (JsonSyntaxException e) {
throw e;
}
return fileParsed;
}
This method was working before generating the jar when I try to run it using the framework(IntelliJ or Eclipse) but If I generate a jar file and then I try to get the json file that is not inside the jar file is not working.
If I execute java -jar file.jar jsonFile.json I'm getting java.lang.NullPointerException
I tried some other ways but always NullPointerException. Any clue about this? Thanks!