Search code examples
javaandroiduniversal-image-loader

Why do I get this "Context = NullPointerException" error in my homework?


I am doing a tutorial for homework, which is to build an Instagram app. The tutorial is about two years old and I am having some problems with the coding.

I am having the following error and am not sure why.

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

My UniversalImageLoader class

public class UniversalImageLoader {

    private static final int defaultImage = R.drawable.ic_android;
    private Context mContext;

    public UniversalImageLoader(Context context) {
        mContext = context;
    }

    public ImageLoaderConfiguration getConfig(){
        //File cacheDir = StorageUtils.getCacheDirectory(mContext);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)//<--the error is in this line
                .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
                .diskCacheExtraOptions(480, 800, null)
                .threadPriority(Thread.NORM_PRIORITY - 2) // default
                .tasksProcessingOrder(QueueProcessingType.FIFO) // default
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024)
                .memoryCacheSizePercentage(13) // default
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheFileCount(100)
                .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
                .imageDownloader(new BaseImageDownloader(mContext)) // default
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
                .writeDebugLogs()
                .build();

        return config;
    }

in HomeActivity:(and OnCreate)[in every Activity I call it like this]

initImageLoader();

private void initImageLoader(){
        UniversalImageLoader universalImageLoader = new UniversalImageLoader(mContext);
        ImageLoader.getInstance().init(universalImageLoader.getConfig());
    }

Solution

  • When you are creating object of UniversalImageLoader class, pass getApplicationContext() instead of activity context.

    Application context is available through out application while activity context is bound to activity life cycle.

    Update:

    Application Context: It is an instance which is the singleton and can be accessed in an activity via getApplicationContext(). This context is tied to the lifecycle of an application. The application context can be used where you need a context whose lifecycle is separate from the current context or when you are passing a context beyond the scope of an activity

    private void initImageLoader(){
        UniversalImageLoader universalImageLoader = new UniversalImageLoader(getApplicationContext());
        ImageLoader.getInstance().init(universalImageLoader.getConfig());
    }
    

    Activity Context This context is available in an activity. This context is tied to the lifecycle of an activity.

    Here read more about the difference in Activity context and application context. https://blog.mindorks.com/understanding-context-in-android-application-330913e32514

    For Multiple Activities you can initialize in Application class onCreate method.

    public class MyApplication extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();
            // Initialize the Universal Image Loader here
    
        DisplayImageOptions defaultOptions = new 
        DisplayImageOptions.Builder()
                .cacheOnDisk(true).cacheInMemory(true).build();
    
        ImageLoaderConfiguration config = new 
        ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions).build();
    
        ImageLoader.getInstance().init(config);
    
    
    }
    

    Then in your Activity get image Loader instance like this.

    ImageLoader mImageLoader = ImageLoader.getInstance();
    

    Also you need to add your Application class in AndroidManifest like this.

    <application
        android:name=".MyApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"