0

I am making an android app and have to save JSON to an SQLite database. The app already has a function to get JSON and display this in a listview and a function to save data to a database. Now I want to combine those, but I am a little lost.

 public void get_data(String data) { try { JSONArray data_array=new JSONArray(data); for (int i = 0 ; i < data_array.length() ; i++) { JSONObject obj=new JSONObject(data_array.get(i).toString()); Courses add=new Courses(); add.name = obj.getString("name"); add.ects = obj.getString("ects"); add.grade = obj.getString("grade"); add.period = obj.getString("period"); courses.add(add); } adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } 

This loops through the JSON so I think this is where is should save to the database.

public boolean insertCourse(String course, int ects, int period, int grade) { SQLiteDatabase db = getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COURSE_COLUMN_COURSE, course); contentValues.put(COURSE_COLUMN_ECTS, ects); contentValues.put(COURSE_COLUMN_PERIOD, period); contentValues.put(COURSE_COLUMN_GRADE, grade); db.insert(COURSE_TABLE_NAME, null, contentValues); return true; } 

This is in a DBHelper.class should be able to use this I think.

I was hoping to reuse the code which i used to save input fields to the database but no luck so far.

 else { if(dbHelper.insertCourse(courseEditText.getText().toString(), Integer.parseInt(ectsEditText.getText().toString()), Integer.parseInt(periodEditText.getText().toString()), Integer.parseInt(gradeEditText.getText().toString()))) { Toast.makeText(getApplicationContext(), "Course Inserted", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(getApplicationContext(), "Could not Insert course", Toast.LENGTH_SHORT).show(); } 

Does anyone have a suggestion how to integrate both (if possible at all). Thnx in advance

EDIT: the logcat crash report:

04-09 17:45:37.204 12244-12244/com.stefspakman.progress2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.stefspakman.progress2, PID: 12244 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.stefspakman.progress2.ProgressDBHelper.insertCourse(java.lang.String, int, int, int)' on a null object reference at com.stefspakman.progress2.gradingActivity.get_data(gradingActivity.java:60) at com.stefspakman.progress2.Download_data$1.handleMessage(Download_data.java:58) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5422) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
5
  • Call insertCourse method when you extract the data from JSON . Commented Apr 9, 2016 at 14:48
  • @PrerakSola I just tried ` String dbCourse = obj.getString("name"); int dbects = Integer.parseInt(obj.getString("ects")); int dbperiod = Integer.parseInt(obj.getString("period")); int dbgrade = Integer.parseInt(obj.getString("grade")); dbHelper.insertCourse(dbCourse,dbects,dbperiod,dbgrade );`, but the app crashes as soon as i open the activity. Commented Apr 9, 2016 at 14:59
  • Post the logcat of the crash in your question. Commented Apr 9, 2016 at 15:09
  • @PrerakSola added the Logcat above Commented Apr 9, 2016 at 15:47
  • @StefSpakman you have null, check the documentation official docs.oracle.com/javase/7/docs/api/java/lang/… Commented Apr 10, 2016 at 8:36

2 Answers 2

1

You need to instantiate your DBHelper class before using the insertCourse Method .

DBHelper helper = new DBHelper(this); helper.insertCourse(Course course); 

Also , its better if you use your Model class object as paramater for database queries .

public boolean insertCourse(Courses courses) { SQLiteDatabase db = getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COURSE_COLUMN_COURSE, courses.getCourse()); contentValues.put(COURSE_COLUMN_ECTS, courses.getEcts()); contentValues.put(COURSE_COLUMN_PERIOD, courses.getPeriod()); contentValues.put(COURSE_COLUMN_GRADE, courses.getGrade()); db.insert(COURSE_TABLE_NAME, null, contentValues); return true; 

}

In this way you can save your data from JSON as

db.insert(courses); 
Sign up to request clarification or add additional context in comments.

1 Comment

I changed the insertCourse but now i get an error "cannot resolve 'getCourse()'"
0

The exception says that your ProgressDBHelper is null, please make sure that you are creating an instance of ProgressDBHelper before calling its methods. Creating an instance is just calling ProgressDBHelper dbHelper = new ProgressDBHelper() and you have to call it before calling dbHelper.insertCourse(...)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.