0

-edit 2. Tested this on my mobile works as intended. Stupid android avd

I am getting a error and i am not quite sure how to fix it. I could remove the need for caching the image but that would increase the bandwidth needed for the application. My current code should work but it is throwing a NullPointerException here is the error

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference
                                                                                           at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:232)
                                                                                           at com.program.programmy.application.FileCache.<init>(FileCache.java:18)

and this is the code causing the problem is the one surrounded by ** line 18

public FileCache(Context context) {
    // Find the dir to save cached images
    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED))
        cacheDir = new File(
                android.os.Environment.getExternalStorageDirectory(),
                "ParseListViewImgTxt");
    else
        **cacheDir = context.getCacheDir();**
    if (!cacheDir.exists())
        cacheDir.mkdirs();
}

if someone could help out with this one little thing my program should work no problem. cheers

-Edit

This is only when i try to leave the list intent to look at a single object. Here is some more code from the area that prompts the error

view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, SingleItemView.class);
                context.startActivity(intent);
            }
        });

Also here is a link to something similar that i am doing except no one is having the problem i am

1 Answers1

0

Whenever your constructor is called, you are not sending any object in context. Exception occurred as context object is null, and you are calling getCacheDir() method on it.

If you have problem in getting context object. Please read top voted answer in following link: Static way to get 'Context' on Android?

Community
  • 1
  • 1