0

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;
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
CP.
  • 1
  • 4
  • what does `response.body()` return? – joao86 Nov 27 '17 at 20:24
  • First, I noticed you have an extra `/` in your URL, remove that and test again, if the issue is still there, can you please share your Crime Model Class? – Mohamed Hamdaoui Nov 27 '17 at 20:27
  • Thanks for your replies. Removed the /, unfortunately a new error comes up: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 93 path $[0].outcome_status. – CP. Nov 27 '17 at 20:51

2 Answers2

0

Make sure this piece of code if working fine.

List<Crime> Crimes = response.body();

Your Crimes List is probably empty or null.

MezzDroid
  • 560
  • 4
  • 11
0

Your response.body() is returning null. I'm pretty sure it's because you misconfigured your Api. As is, you're making an http call to the following uri (notice the double //):

https://data.police.uk/api//crimes-no-location?category=all-crime&force=leicestershire&date=2017-02

Remove the leading slash in your @GET

Jon
  • 1,715
  • 12
  • 14
  • Thanks for your replies. Removed the /, unfortunately a new error comes up: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 93 path $[0].outcome_status. – CP. Nov 27 '17 at 20:54
  • So you had more than one problem. That means there was likely a problem parsing your model. `outcome_status` is expecting a string but it returns a json object instead. You need to update your model to consider that. Look closely at the returned json from your endpoint (I formatted it for you nicely here: https://pastebin.com/tWV7rhvS) – Jon Nov 27 '17 at 21:02