0

Here is a function that is called to display data in a spinner.

This is the response I get. Reponse: Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2.Call co.ke.smartcare.apiutilities.ApiService.fetchDocs()' on a null object reference

Function: public void fetchDoctors(){

    apiService.fetchDocs().enqueue(new Callback<List<Doctor>>() {
        @Override
        public void onResponse( Call<List<Doctor>> call, Response<List<Doctor>> response ) {
            List<Doctor> doctors = new ArrayList<>();
            List<String> docs = new ArrayList<>();

            if( response.body() != null ){

                doctors.addAll(response.body());

                for( int i = 0; i <= doctors.size(); i++ ){
                    docs.add(doctors.get(i).getName());
                }

                ArrayAdapter<String> doctorsSpinner = new ArrayAdapter<>(getApplicationContext(), layout.simple_spinner_item, docs);
                doctorsSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                doctor.setAdapter(doctorsSpinner);
            }else{

                Toast.makeText(BookingActivity.this, "0 doctors have been retrieved. \nKindly, contact customer care", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure( Call<List<Doctor>> call, Throwable t ) {
            Toast.makeText(BookingActivity.this, "Error: "+t.toString(), Toast.LENGTH_SHORT).show();
        }
    });
}
ADM
  • 20,406
  • 11
  • 52
  • 83
Tim Muia
  • 183
  • 1
  • 7
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Feb 23 '21 at 11:10
  • `Attempt to invoke interface method 'retrofit2.Call co.ke.smartcare.apiutilities.ApiService.fetchDocs() on a null object ` – a_local_nobody Feb 23 '21 at 11:10

1 Answers1

0

Your retrofit api call interface must not be initialized properly in that case. Add these in ur build.gradle first.

implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'

Then add this to ur java code.

public static ApiInterface getClient() {
        if(retrofitServerOne==null) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addNetworkInterceptor(loggingInterceptor)
                    .callTimeout(1, TimeUnit.MINUTES)
                    .connectTimeout(1, TimeUnit.MINUTES)
                    .build();
            retrofitServerOne=new Retrofit.Builder()
                    .baseUrl("Url u need")
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(okHttpClient)
                    .build().create(ApiInterface.class);
        }
        return retrofitServerOne;
    }

Try this way it will also show logs for api call. Replace ApiInterface with ur interface.