• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Context = NullPointerException error

Benvanj

Lurker
I am doing a tutoria 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.
error:
..............................................................................................................................................
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;
}

............................................................................................................................................................
I use it in several activities, and call it as a function in the Activity.Class file:
............................................................................................................................................................
private void initImageLoader(){
UniversalImageLoader universalImageLoader = new UniversalImageLoader(mContext);
ImageLoader.getInstance().init(universalImageLoader.getConfig());
}

.............................................................................................................................................................
and in OnCreate I call it like this:
.............................................................................................................................................................
initImageLoader();
............................................................................................................................................................

any information and help will be highly appreciated....I've read about solutions but nothing makes sense...
 
The solution was easy... When you call it in a Fragment, I needed to call the context with :
requireContext()
and in an Activity: context()
 
Back
Top Bottom