-2

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());
    }
  • share the calling of method getConfig(). Did you initialized the Universal image loader sdk? This is the setup guide for reference https://github.com/nostra13/Android-Universal-Image-Loader/wiki/Quick-Setup – TheAnkush Jun 01 '19 at 15:13
  • Apparently, `mContext` is `null`. This implies that you passed `null` to the `UniversalImageLoader` class. – CommonsWare Jun 01 '19 at 15:13
  • @TheAnkush... where? do I have to call it? according to the guide I need to do it in MAinActivity.class... I called it now it fails to open the Activity... – Ben Van Jaarsveld Jun 01 '19 at 15:24
  • @CommonsWare... this sound stupid, but what do I need to pass to it? – Ben Van Jaarsveld Jun 01 '19 at 15:24
  • You need to pass a `Context`. Since this appears to be loading images, your `Activity` would seem to be a likely `Context`. – CommonsWare Jun 01 '19 at 15:40
  • @CommonsWare... But I use this in several Activities... then how do I call only one? – Ben Van Jaarsveld Jun 01 '19 at 15:43
  • show the part of your code where you instantiate UniversalImageLoader(). – Angel Koh Jun 01 '19 at 15:54
  • @ Marcin... please the solution that you posted, I read before I asked the question. It doesn't make sense, hence my question here. I am trying to learn and doesn't understand how it affects my Image Loader.. please help – Ben Van Jaarsveld Jun 01 '19 at 16:50

1 Answers1

0

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"
TheAnkush
  • 885
  • 7
  • 10
  • thanks for the info...It doesn't make any sense... could you please explain use a sample code snippet... – Ben Van Jaarsveld Jun 01 '19 at 15:35
  • 1
    @BenVanJaarsveld - I have updated my answer with code. Observe how I initialized the Universal Image Loader. The code snippet shared by you, value of mContext is not clear. I believe you might have set mContext = . this. – TheAnkush Jun 01 '19 at 21:24