文件创建时出现ANDROID nullpointer错误

时间:2013-09-07 23:51:43

标签: android file nullpointerexception

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

@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 = new ArrayList();               
    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);        
}

logcat的:

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

1 个答案:

答案 0 :(得分:0)

请参阅Android Documentation

在问题中你说“final2.add(read)”得到一个空指针,你确定是这样的吗?这不是堆栈跟踪显示的内容。我认为该行

((read = bufferedReader.readLine()) != null)

确保不会出现这种情况。该声明将按如下方式评估

read = bufferedReader.readLine(); 

从文档中,这将返回“行的内容,如果在读取器结束之前没有读取任何字符,则返回null。”

(expression) != null

然后将该表达式的结果与null进行比较,因此您的代码永远不应该向final2添加null。

我认为你的问题更可能是“findViewById”,它可能会返回null - 再次检查广泛的在线文档。我还会删除SuppressWarnings(“null”)属性

您的代码可能在其他地方出错。如果有疑问,请在整个方法中添加Log.d()并查看logcat中出现的内容...或者调试该方法...至少记录正在读取的行,直到您确定数据已被正确读取为止

我认为你的代码应该是这样的(未经测试的)......

public void loadOpp() {

    // Consider openFileInput(OPPONENT_FILE) instead of the following...
    File opponentFile = new File(getFilesDir(), OPPONENT_FILENAME);
    if (opponentFile.exists()) { // check the file exists
        FileReader reader = new FileReader(opponentFile);
        BufferedReader bufferedReader = new BufferedReader(reader);

        List<String> opponentList = new ArrayList<String>();

        String line = null;
        while ((line = bufferedReader.readLine()) != null) {

            // Attempt some logging (exercise for the reader)
            Log.d(this.getClass().getName(), line);

            opponentList.add(line);
        }

        Spinner spinner = (Spinner) findViewById(R.id.spinner3);
        if (spinner != null) {
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, opponentList);
            dataAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(dataAdapter);
        }
    }
}

要检查的其他事项,您的文件是否存在?它包含任何数据吗?请记住,如果您要创建文件,则应在创建文件之前检查设备上是否有可用空间...等等...

希望有所帮助。