0

I want to sync my SQL Server database with android SQLite.

I have followed the tutorial available on link http://programmerguru.com/android-tutorial/how-to-sync-remote-mysql-db-to-sqlite-on-android/

The difference is I am using Microsoft Visual Studio Web Service in place of PHP.

I have copied the code and made changes as per my requirement. Now, whenever I run the mobile application I am getting the following error:

Unable to start activity ComponentInfo{com.murcyleenpeerzada.mp/com.murcyleenpeerzada.mp.writings}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.murcyleenpeerzada.mp.DBHandler.getAllWriters()' on a null object reference

I have checked the table it has records. The table is not Blank.

My code in DBHandler is:

    public ArrayList<HashMap<String, String>> getAllWriters() {
    ArrayList<HashMap<String, String>> usersList;
    usersList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT * FROM " + MP_TABLE;
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("writeId", cursor.getString(0));
            map.put("title", cursor.getString(1));
            usersList.add(map);
        } while (cursor.moveToNext());
    }
    database.close();
    return usersList;
}

My Activity Code is :

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.writings);

    // Get User records from SQLite DB
    ArrayList<HashMap<String, String>> userList = dbHandler.getAllWriters();
    // If users exists in SQLite DB
    if (userList.size() != 0) {
        // Set the User Array list in ListView
        ListAdapter adapter = new SimpleAdapter(getApplicationContext(), userList, R.layout.writings_controls, new String[] {
                "userId", "userName" }, new int[] { R.id.writings_Title, R.id.writings_Des});
        ListView myList = (ListView) findViewById(android.R.id.list);
        myList.setAdapter(adapter);
    }
Danish_k12
  • 329
  • 2
  • 3
  • 20
  • most likely you haven't initialized dbHandler when you try to do `dbHandler.getAllWriters();` – Jonathan Darryl May 16 '16 at 07:44
  • Hi, I am a newbie can you please tell me how and where to initialize it? Is this the right way? dbHandler=new DBHandler(this); after setContentView – Danish_k12 May 16 '16 at 07:48

0 Answers0