Reputation: 3484
I have defined a Model class along with an interface inside it. The code is as follows:
public class MapModel {
MapContract.Presenter mPresenter;
private Location mResLocation;
private static final String TAG = MapModel.class.getSimpleName();
public MapModel(MapContract.Presenter presenter) {
mPresenter = presenter;
}
public static WebServiceInterface WebServiceInterface = RetrofitServiceGenerator.create(WebServiceInterface.class);
public void queryAddress(String address) throws IOException {
WebServiceInterface .lookupAddress(Constants.GOOGLE_API_KEY, address).enqueue(
new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
mResLocation = response.body().getGeometry().getLocation();
Log.i(TAG, "onResponse: " + response.toString());
Log.i(TAG, "Location : " + mResLocation.getLat() + " : " + mResLocation.getLat());
mPresenter.onSearchSuccess(mResLocation);
}
@Override
public void onFailure(Call<Result> call, Throwable t) {mPresenter.onSearchFailed("Search failed");
}
}
);
}
public interface WebServiceInterface {
@GET("json")
Call<Result> lookupAddress(@Query("key") String apiKey, @Query("address") String searchTerm);
}
}
My retrofit generator is as follows:
public class RetrofitServiceGenerator {
public static <S> S create(Class<S> serviceClass) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor)
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://maps.googleapis.com/maps/api/geocode/")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
// .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return retrofit.create(serviceClass);
}
}
The problem that I have is that when I call the method queryAddress
, it seems to return null. I have created POJO objects for the json result that I get using http://www.jsonschema2pojo.org/. You can see the complete code as well as POJO classes class here. The error that I get is as follows:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Double sematec.mehdi.mymap.webmodels.Location.getLat()' on a null object reference
I think my request is correct because okhttp log shows a correct json response for it. So the culprit is somewhere else. Does anyone know where I am wrong?
Upvotes: 0
Views: 689
Reputation: 3484
I finally found the source of problem. If you look at the web models, there is also a class GoogleMapModel. The calling method must return that type. Next, we'll extract location info from it. So, the code becomes:
public class MapModel {
MapContract.Presenter mPresenter;
private Location mResLocation;
private static final String TAG = MapModel.class.getSimpleName();
public MapModel(MapContract.Presenter presenter) {
mPresenter = presenter;
}
public static WebServiceInterface WebServiceInterface = RetrofitServiceGenerator.create(WebServiceInterface.class);
public void queryAddress(String address) throws IOException {
WebServiceInterface .lookupAddress(Constants.GOOGLE_API_KEY, address).enqueue(
new Callback<GoogleMapModel>() {
@Override
public void onResponse(Call<GoogleMapModel> call, Response<GoogleMapModel> response) {
mResLocation = response.body().getResults().get(0).getGeometry().getLocation();
mPresenter.onSearchSuccess(mResLocation);
}
@Override
public void onFailure(Call<GoogleMapModel> call, Throwable t) {mPresenter.onSearchFailed("Search failed");
}
}
);
}
public interface WebServiceInterface {
@GET("json")
Call<GoogleMapModel> lookupAddress(@Query("key") String apiKey, @Query("address") String searchTerm);
}
}
Upvotes: 1
Reputation: 490
The problem is that you log the lat/long before set the object from response. This line
Log.i(TAG, "Location : " + mResLocation.getLat() + " : " + mResLocation.getLat());
should go after you set the object from the response.
So it should be:
mResLocation = response.body().getGeometry().getLocation();
Log.i(TAG, "Location : " + mResLocation.getLat() + " : " + mResLocation.getLat());
Upvotes: 1