I am working in an android application in which the data is in the form of json and from json data is saved in Sqlite. Till now i have saved the json to sqlite but when i am trying to get data in listview , the application crashes
MainActivity
public class MainActivity extends Activity {
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private EditText editTextName;
SharedPreferences sp;
private String jsonResult;
private ListView listView;
private Button b;
EditText etname, et;
TextView tv;
String myJSON;
private static final String TAG = "MainActivity.java";
private static final String TAG_NAME = "notice";
private CategoryHelper databaseHelper;
Button get, store, select;
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
public static final String USER_NAME = "USERNAME";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noticelist);
select = (Button) findViewById(R.id.button3);
databaseHelper=new CategoryHelper(MainActivity.this);
databaseHelper.getTimeRecordList();
showList();
//SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
// String session_id= myprefs.getString("session_id", null);
//TextView textView = (TextView) findViewById(R.id.fname);
//textView.setText("Welcome "+session_id);
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
select_seqlite();
}
});
// load icons from
// strings.xml
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
//send messages stop
//get json data start
protected void showList(){
try {
JSONArray peoples = new JSONArray(myJSON);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String name=null, date=null;
if(c.has("notice"))
name = c.getString("notice");
databaseHelper.saveCategoryRecord(name);
Cursor c1 = databaseHelper.getTimeRecordList();
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_NAME,c1.getString(0));
personList.add(persons);
}
/*ListAdapter adapter = new SimpleAdapter(
Messages.this, personList, R.layout.list_item,
new String[]{TAG_NAME,TAG_ADD},
new int[]{R.id.name, R.id.address}
);*/
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.list_item1,
new String[]{TAG_NAME},
new int[]{R.id.name}
);
list.setAdapter(adapter);
} catch (JSONException e) {
Log.i("tagconvertstr", "["+myJSON+"]");
}
}
private void select_seqlite() {
// TODO Auto-generated method stub
databaseHelper.getTimeRecordList();
Cursor c = databaseHelper.getTimeRecordList();
if (c.moveToFirst())
{
do {
DisplayContact(c);
} while (c.moveToNext());
}
}
private void DisplayContact(Cursor c) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),"name " + c.getString(0), Toast.LENGTH_LONG).show();
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
String session_id= myprefs.getString("session_id", null);
InputStream inputStream = null;
String result = null;
try {
String postReceiverUrl = "http://ift.tt/1QcQgO5";
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "suyash1"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
inputStream = resEntity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
Log.i("tagconvertstr", "["+result+"]");
System.out.println(e);
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON = result;
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
//get json data stop
}
CatergoryHelper (Sqlite)
public class CategoryHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "category.db";
private static final String TABLE_NAME = "tbcategory";
public static final String CATEGORY_COLUMN_NAME = "name";
Category openHelper;
private SQLiteDatabase database;
public CategoryHelper(Context context){
openHelper = new Category(context);
database = openHelper.getWritableDatabase();
}
public void saveCategoryRecord(String name) {
ContentValues contentValues = new ContentValues();
contentValues.put(CATEGORY_COLUMN_NAME, name);
database.insert(TABLE_NAME, null, contentValues);
}
public Cursor getTimeRecordList() {
return database.rawQuery("select * from " + TABLE_NAME, null);
}
private class Category extends SQLiteOpenHelper {
public Category(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + "( "
+ CATEGORY_COLUMN_NAME + " TEXT )" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
onCreate(db);
}
}
}
Logcat
Process: org.pitechnologies.jsontosqlite, PID: 18136
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pitechnologies.jsontosqlite/org.pitechnologies.jsontosqlite.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONArray.<init>(JSONArray.java:92)
at org.json.JSONArray.<init>(JSONArray.java:108)
at org.pitechnologies.jsontosqlite.MainActivity.showList(MainActivity.java:116)
at org.pitechnologies.jsontosqlite.MainActivity.onCreate(MainActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Aucun commentaire:
Enregistrer un commentaire