1. Are you ready for the Galaxy S20? Here is everything we know so far!

Context = NullPointerException error

Discussion in 'Android Development' started by Benvanj, Jun 1, 2019.

  1. Benvanj

    Benvanj Lurker
    Thread Starter

    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...
     


  2. Best Answer:
    Post #2 by Benvanj, Jun 2, 2019 (1 points)
  3. Benvanj

    Benvanj Lurker
    Thread Starter

    The solution was easy... When you call it in a Fragment, I needed to call the context with :
    requireContext()
    and in an Activity: context()
     
Loading...
Similar Threads - Context NullPointerException error
  1. pratappulugurta
    Replies:
    2
    Views:
    142
  2. M_Leonard
    Replies:
    1
    Views:
    250
  3. RewindJAA
    Replies:
    0
    Views:
    335
  4. nagamothu
    Replies:
    0
    Views:
    453
  5. Daniel Fernandes
    Replies:
    2
    Views:
    1,934
  6. Bhavin392
    Replies:
    1
    Views:
    159
  7. RLScott
    Replies:
    1
    Views:
    347
  8. Seran
    Replies:
    1
    Views:
    1,453
  9. umtblbl
    Replies:
    0
    Views:
    327
  10. karthikkumar cv
    Replies:
    6
    Views:
    1,202

Share This Page

Loading...