I got a java.lang.NullPointerException on this line pdao.majPost(c);
when I try to grab my posts from my API and trying to insert post my SQLite database.
My JSON Parser Class:
private PostsDAO pdao;
public void data() {
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://twitter.192.168.1.38.xip.io/api/v1/posts", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
JSONObject data;
JSONArray posts;
try {
data = new JSONObject(response);
// Traitement des posts
posts = data.getJSONArray("posts");
for (int i = 0; i < posts.length(); i++) {
JSONObject c = posts.getJSONObject(i);
pdao.majPost(c);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
My PostDAO file
public void majPost(JSONObject c) throws JSONException {
ContentValues values = new ContentValues();
values.put("id_post", c.getInt("id_post"));
values.put("id_user", c.getInt("id_user"));
values.put("content", c.getString("content"));
values.put("photo", c.getString("photo"));
values.put("masked", c.getInt("masked"));
values.put("deleted", c.getInt("deleted"));
values.put("created_at", c.getString("created_at"));
values.put("updated_at", c.getString("updated_at"));
db.insert("posts", null, values);
}