15
Мар
2018

java.lang.NullPointerException: Attempt to invoke interface method ‘java.lang.Object java.util.List.get(int)’ on a null object reference

Не могу понять следующую ошибку: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

Base_URL беру из этого сайта

Interface:

public interface RetrofitService {

@GET("entries/count")
Call<List<CountModel>> getJsonList();

}

StartApplication:

public class StartApplication extends Application {

private final String BASE_URL = "https://api.publicapis.org/";
private RetrofitService service;

@Override
public void onCreate() {
    super.onCreate();
    service = initRetrofit();
}

public static StartApplication get(Context context) {

    return (StartApplication) context.getApplicationContext();
}

public RetrofitService getRetrofitClient(){
    return service;
}

private RetrofitService initRetrofit() {
    return new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(RetrofitService.class);
}

}

CountModel:

public class CountModel {

private int count;
private List<JsonModel> entries;

// Getter and Setter

JsonModel:

  @SerializedName("API")
@Expose
private String aPI;
@SerializedName("Description")
@Expose
private String description;
@SerializedName("Auth")
@Expose
private String auth;
@SerializedName("HTTPS")
@Expose
private String hTTPS;
@SerializedName("Cors")
@Expose
private String cors;
@SerializedName("Link")
@Expose
private String link;
@SerializedName("Category")
@Expose
private String category;

// Getter and Setter

MainActivity: Получаю данные с асинхронным запросам.

 private void setRequest() {
    RetrofitService service = StartApplication.get(this).getRetrofitClient();
    Call<List<CountModel>> call = service.getJsonList();
    call.enqueue(new Callback<List<CountModel>>() {
        @Override
        public void onResponse(Call<List<CountModel>> call, Response<List<CountModel>> response) {
            List<CountModel> list = response.body();
            CountModel model = list.get(0);
           // Log.d("Success", String.valueOf(model.getEntries()));
            Log.d("Success", String.valueOf(model.getCount()));

            ArrayList<String> stringList = new ArrayList<>();
            for (int i = 0; i < 20 ; i++) {
             //   stringList.add(String.valueOf(list.get(i).getEntries()));
                stringList.add(String.valueOf(list.get(0).getCount()));
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_list_item_1, stringList);
            listView.setAdapter(adapter);

        }

        @Override
        public void onFailure(Call<List<CountModel>> call, Throwable t) {

        }
    });

Далее получаю следуюшую ошибку:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
                                                              at example.cwpo18.MainActivity$1.onResponse(MainActivity.java:45)

MainActivity.java:45

 CountModel model = list.get(0);

Может я не правильно получаю данные ?

Источник: https://ru.stackoverflow.com/questions/798393/java-lang-nullpointerexception-attempt-to-invoke-interface-method-java-lang-ob

Тебе может это понравится...

Добавить комментарий