I am trying to fetch an array from the data.police.uk website.
Unfortunately, when running the code an error comes up: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
Here's the code, no idea, what I am doing wrong:
package com.example.cosmin.crimerate.Latest_crimes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.cosmin.crimerate.R;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class JsonCrimesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_crimes);
final ListView listView = findViewById(R.id.crimesList);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<List<Crime>> call = api.getCrimes();
call.enqueue(new Callback<List<Crime>>() {
@Override
public void onResponse(Call<List<Crime>> call, Response<List<Crime>> response) {
List<Crime> Crimes = response.body();
String[] crimeCategories = new String[Crimes.size()];
for (int i=0; i < Crimes.size();i++){
crimeCategories[i]= Crimes.get(i).getCategory();
}
listView.setAdapter(
new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_list_item_1,
crimeCategories
)
);
}
@Override
public void onFailure(Call<List<Crime>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
That's the Api class:
public interface Api{
String BASE_URL = "https://data.police.uk/api/";
@GET("/crimes-no-location?category=all-crime&force=leicestershire&date=2017-02")
Call<List<Crime>> getCrimes();
Do you think it can be because of the array?
If you can please me give me a hint or anything what's happening.
Thank you for your help!
LE: CRIME MODEL:
public class Crime {
private String category;
private String persistent_id;
private String location_subtype;
private String id;
private String location;
private String context;
private String month;
private String location_type;
private String outcome_status;
public Crime(String category, String persistent_id, String location_subtype, String id, String location, String context, String month, String location_type, String outcome_status) {
this.category = category;
this.persistent_id = persistent_id;
this.location_subtype = location_subtype;
this.id = id;
this.location = location;
this.context = context;
this.month = month;
this.location_type = location_type;
this.outcome_status = outcome_status;
}
public String getCategory() {
return category;
}
public String getPersistent_id() {
return persistent_id;
}
public String getLocation_subtype() {
return location_subtype;
}
public String getId() {
return id;
}
public String getLocation() {
return location;
}
public String getContext() {
return context;
}
public String getMonth() {
return month;
}
public String getLocation_type() {
return location_type;
}
public String getOutcome_status() {
return outcome_status;
}
}