0

在 while 语句中, final2.add(read) 得到一个空指针,这让我相信它没有正确读取文件,所以我的问题是以下我是否正确获取了文本文件的内容?

@SuppressWarnings("null")
public void loadOpp() throws FileNotFoundException {
    File y = new File(getFilesDir()+File.separator+"Opponent.txt");
    FileReader x = new FileReader(y);
    BufferedReader bufferedReader = new BufferedReader(x);
    String read;

    List<String> final2 = null;               
    try {
        while((read = bufferedReader.readLine()) != null){
            final2.add(read);
          }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Spinner spinner= (Spinner) findViewById(R.id.spinner3);
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, final2);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);        
}

日志猫:

09-06 21:43:00.874: E/AndroidRuntime(779): FATAL EXCEPTION: main
09-06 21:43:00.874: E/AndroidRuntime(779): java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.hoopstats/com.example.hoopstats.NewGame}: java.lang.NullPointerException
09-06 21:43:00.874: E/AndroidRuntime(779):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-06 21:43:00.874: E/AndroidRuntime(779):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-06 21:43:00.874: E/AndroidRuntime(779):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-06 21:43:00.874: E/AndroidRuntime(779):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-06 21:43:00.874: E/AndroidRuntime(779):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 21:43:00.874: E/AndroidRuntime(779):  at android.os.Looper.loop(Looper.java:137)
09-06 21:43:00.874: E/AndroidRuntime(779):  at android.app.ActivityThread.main(ActivityThread.java:5103)
09-06 21:43:00.874: E/AndroidRuntime(779):  at java.lang.reflect.Method.invokeNative(Native Method)
09-06 21:43:00.874: E/AndroidRuntime(779):  at java.lang.reflect.Method.invoke(Method.java:525)
09-06 21:43:00.874: E/AndroidRuntime(779):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-06 21:43:00.874: E/AndroidRuntime(779):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-06 21:43:00.874: E/AndroidRuntime(779):  at dalvik.system.NativeStart.main(Native Method)
09-06 21:43:00.874: E/AndroidRuntime(779): Caused by: java.lang.NullPointerException
09-06 21:43:00.874: E/AndroidRuntime(779):  at com.example.hoopstats.NewGame.loadOpp(NewGame.java:67)
09-06 21:43:00.874: E/AndroidRuntime(779):  at com.example.hoopstats.NewGame.onCreate(NewGame.java:34)
09-06 21:43:00.874: E/AndroidRuntime(779):  at android.app.Activity.performCreate(Activity.java:5133)
09-06 21:43:00.874: E/AndroidRuntime(779):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-06 21:43:00.874: E/AndroidRuntime(779):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-06 21:43:00.874: E/AndroidRuntime(779):  ... 11 more
4

2 回答 2

2

您需要初始化您的 final2 变量。

List<String> final2 = new List<String>();

代替

List<String> final2 = null;

没有 .add() 方法可以为 null

于 2013-09-05T22:53:45.057 回答
0

你永远不会初始化final2. 你在这里声明

List<String> final2 = null;

就在 the 之前,loop但您在尝试调用add()此处之前从未对其进行初始化

final2.add(read);

将其更改为

List<String> final2 = new List<String>();

此外,为了将来参考,当您遇到崩溃时,请在您的问题中发布 logcat。它几乎总是让我们更容易找到您的问题所在。

于 2013-09-05T22:53:37.163 回答