hi I'm trying to save a location with sharedpreferences but when I want to load it I can not.
The problem is in the load in the default value that I put that is null but I do not know that another value put probe with several things but I do not take it either
If you have any solution, either a default value or another way of saving it with shared preferences, I would appreciate it
this is my shared preferences code.
public void guardarpreferencias() {
SharedPreferences preferences = getSharedPreferences("localizaciones", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
String json = gson.toJson(loccasa);
editor.putString("localizacionC", json);
json = gson.toJson(loctrabajo);
editor.putString("localizacionT", json);
json = gson.toJson(locfacultad);
editor.putString("localizacionF", json);
editor.apply();
}
public void cargarpreferencias(){
SharedPreferences preferences = getSharedPreferences("localizaciones", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = preferences.getString("localizacionC", null);
Type type = new TypeToken<Location>() {
}.getType();
loccasa = gson.fromJson(json, type);
json = preferences.getString("LocalizacionT", null);
type = new TypeToken<Location>() {
}.getType();
loctrabajo = gson.fromJson(json, type);
json = preferences.getString("LocalizacionF", null);
type = new TypeToken<Location>() {
}.getType();
locfacultad = gson.fromJson(json, type);
}
this would be the error that throws me
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'float android.location.Location.distanceTo (android.location.Location)' on a null object reference
When I try to use the variable that loads it can not because it is null, for the first time I do not know what value to put default