udacity / ud851-Exercises

Apache License 2.0
2.04k stars 6.81k forks source link

Why do I get this “Context = NullPointerException” error in my homework? #454

Closed Benvanj closed 5 years ago

Benvanj commented 5 years ago

I am doing a tutorial 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...