private void getNewPhotos() {
Call<JsonElement> call1 = RestClient.post().getNewPhotos(Config.NEW_ID, Config.unsplash_access_key);
call1.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
// progressDialog.dismiss();
newPhotoslist.clear();
Log.e("FeatureNews", response.body().toString());
if (response.isSuccessful()) {
JSONArray jsonArr = null;
try {
jsonArr = new JSONArray(response.body().toString());
if (jsonArr.length() > 0) {
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject json2 = jsonArr.getJSONObject(i);
String id=json2.getString("id");
JSONObject object=json2.getJSONObject("urls");
String url=object.getString("regular");
JSONObject objectUser=json2.getJSONObject("user");
JSONObject objectUserProfile=objectUser.getJSONObject("profile_image");
String userprofile=objectUserProfile.getString("large");
newPhotoslist.add(new PhotosBean(id,url,userprofile));
}
bindTrendData();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Asked
Active
Viewed 1,668 times
-1

kingston
- 11,053
- 14
- 62
- 116

Roshan Adhikari
- 1
- 2
-
Seriously? The error captured in the image is telling you EXACTLY what's wrong. In this chain `response.body().toString()` either `response` object is `null` (doubtful) or the call to `body()` is returning null. And so, you can call any method (in this case `toString()`) on a `null` reference. – hfontanez Jan 14 '22 at 18:40
-
Actually the error already says that it is not response to be null but the body – kingston Jan 14 '22 at 18:45
-
please clarify and write code – Roshan Adhikari Jan 14 '22 at 18:58
1 Answers
0
You should move the log after the check for success. In general, unless you have a good reason to assume that a variable is not null it is better to check.
By the way you don't need to explicitly call the toString
you can do something like ""+response.body()
and eventually you will get null
.

kingston
- 11,053
- 14
- 62
- 116