0

I'm trying to use Retrofit & OKHttp to cache HTTP responses. But I'm Getting java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir() on a null object reference. Here I will put my API Java Class.Please help me,your Help will be Appreciated.

Here is my ApiManager class which contains the interceptor , the getAPI() which has the retrofit building part:

public class ApiManager {


    private static Context context;

    public static final String API_URL = "https://www.alot.com/webservice/"; //Production

    private static ProductionAPIService service;

    public ProductionAPIService getService() {
        return service;
    }


    private static OkHttpClient okClient;
    private static ApiManager apiManager = null;

    private ApiManager() {
    }

    public static ApiManager getApi() {
        if (apiManager == null) {
            apiManager = new ApiManager();





            File httpCacheDirectory = new File(context.getCacheDir(), "responses");
            int cacheSize = 10 * 1024 * 1024; // 10 MiB
            Cache cache = new Cache(httpCacheDirectory, cacheSize);


            OkHttpClient client = new OkHttpClient.Builder()
                    .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
                    .cache(cache)
                    .build();

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(API_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    //    .client(SelfSigningClientBuilder.createClient(MyApplication.context))
                    .client(client)
                    .build();


            service = retrofit.create(ProductionAPIService.class);
        }
        return apiManager;
    }

    private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            if (AppUtils.isConnectedToInternet(context,true)) {
                int maxAge = 60; // read from cache for 1 minute
                return originalResponse.newBuilder()
                        .header("Cache-Control", "public, max-age=" + maxAge)
                        .build();
            } else {
                int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                return originalResponse.newBuilder()
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .build();
            }
        }


    };
}

1 Answers1

0

NullPointerException is thrown when there's no object held in memory assigned to your variable. In other words you didn't assign any Context to your context variable. You should pass it in your static getApi() method. The end result should look like this

 public static ApiManager getApi(@NonNull Context context) {
    if (apiManager == null) {
        apiManager = new ApiManager();

        this.context = context;

        File httpCacheDirectory = new File(context.getCacheDir(), "responses");
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        Cache cache = new Cache(httpCacheDirectory, cacheSize);


        OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
                .cache(cache)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                //    .client(SelfSigningClientBuilder.createClient(MyApplication.context))
                .client(client)
                .build();


        service = retrofit.create(ProductionAPIService.class);
    }
    return apiManager;
}