0

I have an ArrayList of testobject And I want to save it in sqlite database.

class testobject {
      public static String x="";
      public staticint y=0;
      public static boolean z=false;

}

after some search I found this example And I use it.

 ArrayList<testobject> items=new ArrayList<testobject()>;
 items.add(new testobject());
//To insert,

JSONObject json = new JSONObject();
json.put("uniqueArrays", new JSONArray(items));
String arrayList = json.toString();
// Insert the string into db.

//To Read, Read the string from db as String,

JSONObject json = new JSONObject(stringreadfromsqlite);
ArrayList items = json.optJSONArray("uniqueArrays");
Log.d("","itemssize="+items.size);

but the problem is when I want to read my ArrayList from database, it gives this error message for this line "Log.d("","itemssize="+items.size);":

java.long.NullPointException    

actually inested of this line "JSONObject json = new JSONObject(stringreadfromsqlite);" I use "JSONObject json = new JSONObject();" is it myproblem for that?
whats wrong?
as @satur9nine said its dosn't possible to convert my ArrayList to json. so how can i save my arraylist?

Community
  • 1
  • 1
max
  • 5,963
  • 12
  • 49
  • 80
  • What line of code gives the NPE? – Code-Apprentice Jun 01 '14 at 01:12
  • The JSONObject class is not going to be able to convert your `testobject` into JSON. JSONObject and JSONArray only are able to process basic Java types like String, long, boolean, etc. Do you have a good reason why you want to store JSON in SQLite? It's not very useful since you can not search or sort the data easily, you might as well use SharedPreferences. – satur9nine Jun 01 '14 at 04:22
  • i update my question checkout. – max Jun 01 '14 at 09:51

0 Answers0