我有一个字符串对象。我使用bufferedReader.readline()
写信给它。我已经注销了s的内容,在logcat中我可以看到它们,所以我知道它不是空的。我后来使用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));
}
答案 0 :(得分: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");