Reputation: 45
MAIN ACTIVITY
When i try to debug the app its not entering in to the on Respone method, its crashing before that .
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<EarthQuakeObject> earthquakes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
earthquakes =getEarthquakeData();
EarthQuakeAdapter adapter = new EarthQuakeAdapter(MainActivity.this,
earthquakes);
LinearLayoutManager gridLayoutManager = new
LinearLayoutManager(MainActivity.this);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(adapter);
}
public ArrayList<EarthQuakeObject> getEarthquakeData() {
EarthquakeApi earthquakeApi =
RetrofitClient.getRetrofitInstance().create(EarthquakeApi.class);
Call<JSONObject> call = earthquakeApi.getEarthquakeInfo();
Log.e("MainActivity", String.valueOf(call.request().body()));
call.enqueue(new Callback<JSONObject>() {
@Override
public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
if(response.isSuccessful()) {
JSONObject responseObject = response.body();
try {
JSONArray array = responseObject.getJSONArray("features");
for (int i = 0; i < array.length(); i++) {
JSONObject completeData = array.getJSONObject(i);
JSONObject properties = completeData.getJSONObject("properties");
double magnitude = properties.getDouble("mag");
double time = properties.getDouble("time");
String place = properties.getString("place");
EarthQuakeObject earthQuakeObject = new EarthQuakeObject(magnitude,place,time);
earthquakes.add(earthQuakeObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<JSONObject> call, Throwable t) {
System.out.println(t.getMessage());
}
});
return earthquakes;
}
}
ADAPTER CLASS
Its showing a null pointer exception - java.lang.NullPointerException: Attempt to invoke virtual method int java.util.ArrayList.size() on a null object reference
'public class EarthQuakeAdapter extends RecyclerView.Adapter<EarthQuakeAdapter.EarthQuakeViewHolder>{
private Context mctx;
private ArrayList<EarthQuakeObject> earthQuakeObjectList;
public EarthQuakeAdapter(Context mctx, ArrayList<EarthQuakeObject> earthQuakeObjectList) {
this.mctx = mctx;
this.earthQuakeObjectList = earthQuakeObjectList;
}
@NonNull
@Override
public EarthQuakeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mctx).inflate(R.layout.grid_layout,parent,false);
return new EarthQuakeViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull EarthQuakeViewHolder holder, int position) {
EarthQuakeObject earthQuakeObject = earthQuakeObjectList.get(position);
holder.mag.setText(""+earthQuakeObject.getmMagnitude());
holder.place.setText(earthQuakeObject.getmPlace());
holder.date.setText(""+earthQuakeObject.getmDate());
}
@Override
public int getItemCount() {
return earthQuakeObjectList.size();
}
public class EarthQuakeViewHolder extends RecyclerView.ViewHolder {
TextView mag;
TextView place;
TextView date;
public EarthQuakeViewHolder(@NonNull View itemView) {
super(itemView);
mag =itemView.findViewById(R.id.mag);
place = itemView.findViewById(R.id.place);
date = itemView.findViewById(R.id.date);
}
}
}
MODEL CLASS
public class EarthQuakeObject implements Serializable {
@SerializedName("mag")
private double mMagnitude;
@SerializedName("place")
private String mPlace;
@SerializedName("time")
private double mDate;
public EarthQuakeObject(double mMagnitude, String mPlace, double mDate) {
this.mMagnitude = mMagnitude;
this.mPlace = mPlace;
this.mDate = mDate;
}
public double getmMagnitude() {
return mMagnitude;
}
public void setmMagnitude(double mMagnitude) {
this.mMagnitude = mMagnitude;
}
public String getmPlace() {
return mPlace;
}
public void setmPlace(String mPlace) {
this.mPlace = mPlace;
}
public double getmDate() {
return mDate;
}
public void setmDate(double mDate) {
this.mDate = mDate;
}
}'
RETROFIT CLASS
public class RetrofitClient {
private static Retrofit retrofit;
private static final String base_url ="https://earthquake.usgs.gov";
public static Retrofit getRetrofitInstance(){
if(retrofit== null){
retrofit = new Retrofit.Builder()
.baseUrl(base_url)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
INTERFACE
public interface EarthquakeApi {
@GET("/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-01-31&minmag=6&limit=10")
Call<JSONObject> getEarthquakeInfo();
}
ERROR LOG
2019-09-26 19:38:10.724 6634-6634/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.quakereport, PID: 6634
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.example.quakereport.EarthQuakeAdapter.getItemCount(EarthQuakeAdapter.java:46)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3834)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Upvotes: 0
Views: 647
Reputation: 11
You got an empty array list from the response. Here are some changes in your api request
Call<JsonObject> call = earthquakeApi.getEarthquakeInfo();
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response)
{
if (response.isSuccessful()) {
JSONObject responseObject = response.body();
try {
JsonArray jsonArray response.body().get("features").getAsJsonArray();
Log.e("Response.....", "-----//array//----" + jsonArray);
for (int i = 0; i < jsonArray.size(); i++) {
Log.e("TAG", "---item---" + jsonArray.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<JSONObject> call, Throwable t) {
System.out.println(t.getMessage());
}
});
I just print the array objects list to show the data. You can pass this list to recycler view.
Hope, it works for you!!
Upvotes: 0
Reputation: 702
Init. your ArrayList in MainActivity before this earthquakes =getEarthquakeData();
like ArrayList<> earthquakes= new ArraryList<>();
you are seeing this error(java.lang.NullPointerException) because earthQuakeObjectList.size(); is null.
Upvotes: 1