Java getFileDir()导致空指针异常

Java getFileDir()导致空指针异常,java,android,file,Java,Android,File,我刚开始学习Java,但希望有人能解释我在这里做错了什么,无数的谷歌搜索让我头晕目眩 我一直在遵循一个教程,该教程演示了如何将ArrayList保存到文本文件,然后获取详细信息 我收到以下错误:- 01-10 19:00:37.092 16897-16897/uk.co.c2digital.futurefoto E/AndroidRuntime: FATAL EXCEPTION: main Process: uk.co.c2digital.futurefoto, PID: 16897 java.

我刚开始学习Java,但希望有人能解释我在这里做错了什么,无数的谷歌搜索让我头晕目眩

我一直在遵循一个教程,该教程演示了如何将ArrayList保存到文本文件,然后获取详细信息

我收到以下错误:-

01-10 19:00:37.092 16897-16897/uk.co.c2digital.futurefoto E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.co.c2digital.futurefoto, PID: 16897
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{uk.co.c2digital.futurefoto/uk.co.c2digital.futurefoto.viewLocations}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:201)
at uk.co.c2digital.futurefoto.fileControl.<init>(fileControl.java:23)
at uk.co.c2digital.futurefoto.viewLocations.<init>(viewLocations.java:18)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5696) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
01-10 19:05:37.142 16897-16897/uk.co.c2digital.futurefoto D/Process: killProcess, pid=16897
01-10 19:05:37.162 16897-16897/uk.co.c2digital.futurefoto D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 
FileControl();位于名为“fileControl.java”的单独文件中。

public class fileControl extends AppCompatActivity {


    // Set filename
    String filename = getFilesDir() + "/savedList.txt";

    /**
     * Following code gets a list of saved image paths from device
     * @return

     */


    public ArrayList getSaved() {


        // Create variable for storing each line as it's read
        String line;

        // Clear imgArray ready for new entries
        ArrayList newList = new ArrayList();
        newList.clear(); // ensure new array is clear before continuing

        // Try to open file
        try {
            BufferedReader input = new BufferedReader(new FileReader(filename));
            if (!input.ready()) {
                // If input file is not ready, show log error
                Log.d("getSavedList", "Unable to open save file, input not ready");
            } else {

                // If input file is ready, read each line one at a time and add to array
                while ((line = input.readLine()) != null) {
                    newList.add(line);
                }
                input.close();

                // Run verification on results to ensure no duplicates or deleted images are recorded
                newList = listVerify(newList);

            }
        } catch (IOException e) {
            Log.d("getSavedList", "Read save file failed: " + e);
        }

        return newList;
    }

这里有很多错误。首先,
Activity
类,或者在您的情况下,
AppCompatActivity
类通常用于显示带有UI的屏幕,而不是执行谨慎的任务。您的
getSaved()
函数应该只驻留在实际包含UI的活动中

其次,
AppCompatActivity
类不能静态执行上下文操作。换句话说,您的活动需要先实例化,然后才能使用其超类方法,在本例中为
getFilesDir()

归根结底,您需要在生命周期函数中初始化
filename
,可能是
onCreate()

public class fileControl extends AppCompatActivity {


    // Set filename
    String filename = getFilesDir() + "/savedList.txt";

    /**
     * Following code gets a list of saved image paths from device
     * @return

     */


    public ArrayList getSaved() {


        // Create variable for storing each line as it's read
        String line;

        // Clear imgArray ready for new entries
        ArrayList newList = new ArrayList();
        newList.clear(); // ensure new array is clear before continuing

        // Try to open file
        try {
            BufferedReader input = new BufferedReader(new FileReader(filename));
            if (!input.ready()) {
                // If input file is not ready, show log error
                Log.d("getSavedList", "Unable to open save file, input not ready");
            } else {

                // If input file is ready, read each line one at a time and add to array
                while ((line = input.readLine()) != null) {
                    newList.add(line);
                }
                input.close();

                // Run verification on results to ensure no duplicates or deleted images are recorded
                newList = listVerify(newList);

            }
        } catch (IOException e) {
            Log.d("getSavedList", "Read save file failed: " + e);
        }

        return newList;
    }