我正在尝试读取其中包含 json 格式内容的 txt 文件,我正在使用异步任务从资产文件夹中读取文件,但得到空指针异常。下面是我的代码。
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
private MyDBAdapter dbHelper;
String fileName = "json.txt";
Context c;
private static final String result = null;
ArrayList<HashMap<String, String>> arraylist;
@Override
protected Void doInBackground(Void... params) {
readFileFromAssets(fileName,c);
return null;
}
public static String readFileFromAssets(String fileName, Context c) {
AssetManager assetManager = c.getAssets();
InputStream is = null;
try {
is = assetManager.open(fileName);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text = new String(buffer);
System.out.println("tex===========t"+ text);
return text;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
下面是我的日志跟踪
09-24 10:53:25.430: E/AndroidRuntime(1714): Caused by: java.lang.NullPointerException
09-24 10:53:25.430: E/AndroidRuntime(1714): at com.markupartist.android.actionbar.example.DownloadJSON.readFileFromAssets(DownloadJSON.java:75)
09-24 10:53:25.430: E/AndroidRuntime(1714): at com.markupartist.android.actionbar.example.DownloadJSON.doInBackground(DownloadJSON.java:27)
09-24 10:53:25.430: E/AndroidRuntime(1714): at com.markupartist.android.actionbar.example.DownloadJSON.doInBackground(DownloadJSON.java:1)
09-24 10:53:25.430: E/AndroidRuntime(1714): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-24 10:53:25.430: E/AndroidRuntime(1714): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-24 10:53:25.430: E/AndroidRuntime(1714): ... 5 more
不知道我在哪里做错了。