我的txt文件包含JSON内容,此文件的大小为88KB,现在我正在尝试读取此文件内容,但我收到Nullpointerexception
错误
这是我的代码和logcat。请帮忙。
我发布了我的解析JSONcode
InputStream stream = getResources().openRawResource(R.raw.topiclist);
readTextFile(stream);//While debugging i got sourcenotfound on this line
public static void readTextFile(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
Log.w("LOG", e.getMessage());
} finally {
try {
is.close();
} catch (IOException e) {
Log.w("LOG", e.getMessage());
}
}
topic=new GetTopicList();
topic.parseJson(sb.toString());
}
Logcat
02-27 22:29:45.170: E/AndroidRuntime(2099): FATAL EXCEPTION: main
02-27 22:29:45.170: E/AndroidRuntime(2099): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.postreply/com.example.postreply.PostReply}: java.lang.NullPointerException
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.os.Looper.loop(Looper.java:137)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-27 22:29:45.170: E/AndroidRuntime(2099): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 22:29:45.170: E/AndroidRuntime(2099): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 22:29:45.170: E/AndroidRuntime(2099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-27 22:29:45.170: E/AndroidRuntime(2099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-27 22:29:45.170: E/AndroidRuntime(2099): at dalvik.system.NativeStart.main(Native Method)
02-27 22:29:45.170: E/AndroidRuntime(2099): Caused by: java.lang.NullPointerException
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-27 22:29:45.170: E/AndroidRuntime(2099): at com.tomovwgti.socketio.SQLiteAdapter.openToWrite(SQLiteAdapter.java:56)
02-27 22:29:45.170: E/AndroidRuntime(2099): at com.tomovwgti.socketio.GetTopicList.parseJson(GetTopicList.java:126)
02-27 22:29:45.170: E/AndroidRuntime(2099): at com.example.postreply.PostReply.readTextFile(PostReply.java:200)
02-27 22:29:45.170: E/AndroidRuntime(2099): at com.example.postreply.PostReply.onCreate(PostReply.java:86)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.app.Activity.performCreate(Activity.java:5008)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-27 22:29:45.170: E/AndroidRuntime(2099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-27 22:29:45.170: E/AndroidRuntime(2099): ... 11 more
parseJson():
public void parseJson(String response)
{
sqlAdapter=new SQLiteAdapter(context);
sqlAdapter.openToWrite();
//long s=sqlAdapter.countjournals();
JSONArray ja = null;
JSONObject json=null;
String status = null;
try {
json=new JSONObject(response);
status=json.getString("Status");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(status.equals("0"))
{
String addtopics;
try {
addtopics = json.getString("AddTopics");
ja = new JSONArray(addtopics);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int len=ja.length();
topicid = new String[len];
topic = new String[len];
field = new String[len];
subject = new String[len];
topiccode = new String[len];
topicStatus = new String[len];
JSONObject jo;
for(int j=0;j<len;j++)
{
try
{
jo = ja.getJSONObject(j);
topicid[j] = jo.getString("TopicID");
field[j] = jo.getString("Field");
subject[j] = jo.getString("Subject");
topic[j] = jo.getString("Subcategory");
topiccode[j] = jo.getString("TopicCode");
topicStatus[j] = jo.getString("TopicStatus");
sqlAdapter.insert(topicid[j], field[j], subject[j], topic[j], topiccode[j], topicStatus[j]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sqlAdapter.close();
}
}
答案 0 :(得分:0)
此行正在生成错误:
sqlAdapter.openToWrite();
表示sqlAdapter
为null
。这可能是因为它在
sqlAdapter=new SQLiteAdapter(context);
返回null
值。你需要找出发生这种情况的原因并加以解决。