Android Retrofit错误 - 尝试在空对象引用上调用接口方法'int java.util.List.size()'

时间:2016-12-03 15:05:27

标签: android json nullpointerexception retrofit

我正在尝试使用Retrofit for Android通过http获取JSON数据。我收到以下错误:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

我正在尝试从JSON结果中检索businessName和city。
(searchListings-> searchListing-> businessName& searchListings-> searchListing-> city)

这是我的课程。

客户端:

public class RestClient {

    public static final String BASE_URL2 = "http://pubapi.yp.com/";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL2)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

接口:

public interface SearchAPIService {
    @GET("/search-api/search/devapi/search?searchloc=30043&term=barbers&format=json&sort=distance&radius=1&listingcount=20&key=gmj3x7mhsh")
    public
        //End Url
    Call<SearchList> fetchShopsList();
}

型号:

public class Search {

    String businessName;
    String city;

    public String getName() {
        return businessName;
    }

    public String getCity() {
        return city;
    }
}

要保留列表的模型:

public class SearchList {

    List<Search> searchListing;

    public List<Search> getShops() {
        return searchListing;
    }
}

打印结果的MainActivity

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    SearchAPIService apiSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        apiSearch = RestClient.getClient().create(SearchAPIService.class);
        fetchSearchList();
    }

    private void fetchSearchList() {
        Call<SearchList> call = apiSearch.fetchShopsList();
        call.enqueue(new Callback<SearchList>() {
            @Override
            public void onResponse(Call<SearchList> call, Response<SearchList> response) {
                Log.d(TAG, "Total number fetched : " + response.body().getShops().size());

                for(Search search : response.body().getShops()){
                    //Print out Business Name and City in log
                    Log.d(TAG, "Name: " + search.getName());
                    Log.d(TAG, "City: " + search.getCity());
                }

            }


            @Override
            public void onFailure(Call<SearchList> call, Throwable t) {
                Log.e(TAG, "Got error : " + t.getLocalizedMessage());
            }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

这是您要解析的JSON。 (休息修剪)

{
    "searchResult": {
        "searchListings": {
            "searchListing": [{
                "businessName": "Playaz Barbershop",
                "city": "Lawrenceville"
            }, {
                "businessName": "Upscale Barbers",
                "city": "Lawrenceville"
            }, {
                "businessName": "All About You Hair Salon",
                "city": "Lawrenceville"
            }]
        }
    }
}

好像你的POJO课程错了。这就是你获得null的原因。请参阅以下课程。

SearchList.java

public class SearchList {
    public SearchResult searchResult;

    // getters and setters
}

SearchResult.java

public class SearchResult {
    public SearchListings searchListings;

    // getters and setters
}

SearchListings.java

public class SearchListings {
    public List<SearchListing> searchListing = new ArrayList<SearchListing>();

    // getters and setters
}

SearchListing.java

public class SearchListing {
    public String businessName;
    public String city;

    // getters and setters
}

使用这些POJO类而不是POJO类。

onResponse内,

@Override
public void onResponse(Call<SearchList> call, Response<SearchList> response) {
    Log.e("ResponseSize", response.body().getSearchResult().getSearchListings().getSearchListing().size());
}