我正在編寫讀取json中的文件並將其轉換爲散列表的代碼。將字符串轉換爲hasthtable會產生NullPointerException
但是我有一個NullPointerException
當我嘗試將字符串轉換爲散列表。
readBackInfo = gson.fromJson(readjsonString, fileInfoType);
下面是我執行
public void readSyncFile(){
String readjsonString = null;
readBackInfo = new HashMap<String, ArrayList<String[]>>();
try {
FileInputStream fstream = new FileInputStream(_dirName+"/."+_dirName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
if(readjsonString==null){
readjsonString = strLine;
} else {
readjsonString = readjsonString + "\n" + strLine;
}
}
in.close();
//System.out.println(readjsonString);
Type fileInfoType = new TypeToken<HashMap<String, ArrayList<String[]>>>() {}.getType();
System.out.println(allFiles.isEmpty());
//getting a NullPointerException from the line below!
readBackInfo = gson.fromJson(readjsonString, fileInfoType);
sFileInToHashtable();
//readjsonString = gson.toJson(readBackInfo);
//System.out.println(readjsonString);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
當提問有關獲取異常的問題時,在問題中包含堆棧跟蹤總是一個好主意。 –