0

文字列オブジェクトがあります。を使用して書き込みbufferedReader.readline()ます。sの内容をログアウトしましたが、logcatでそれらを確認できるので、nullではないことがわかります。後でsを使用して呼び出しますString.splitが、NPEを取得しています。なぜ、すでに入力されているのでnullではないのですか?

ありがとう。

String cachePath = getCacheDir().getAbsolutePath();
        FileReader fr = null;
        try {
            fr = new FileReader(cachePath + "/dbcache/" + "cacheTextFile.txt");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader(fr);
        String s = null;
        try {
            while((s = br.readLine()) != null) {
            Log.e(TAG, "s = " + s);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {

            fr.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 



        HashMap<String, String> hash = new HashMap<String, String>();
        Log.e(TAG, "about to call on s"+s.length());
        String[] arr = s.split(",");

        for(int i = 0; i < arr.length; i=i+2){
            String key = arr[i].toString();
            hash.put(key, "-1");
            Log.e(TAG, "hash key = " + hash.get(key));
        }
4

1 に答える 1

3

では、while常に文字列をオーバーライドしているのでs、最後に到達すると、次のようになります。null

 while((s = br.readLine()) != null) {
    Log.e(TAG, "s = " + s);
 }

代わりにこれを行ってください。

 StringBuilder stringBuilder = new StringBuilder();
 String currentLine = null;
  while((currentLine = br.readLine()) != null)
        stringBuilder.append(currentLine + "\n");
于 2013-02-06T14:23:54.220 に答える