Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:04:08+00:00 2026-06-15T15:04:08+00:00

I am facing problem when i am expecting callback on onCreate() of SQLiteOpenHelper. I

  • 0

I am facing problem when i am expecting callback on onCreate() of SQLiteOpenHelper.
I have gone through many of related question posted here and tried to implement the suggestion but problem still persist.

Problem is:
I am getting a NullPointerException when i am calling getWritableDatabase().
I tried to put log in onOpen(), onUpgrade(), onCreate().

I used breakpoint also but could not get any thing more than exception.

Logs are below.
12-08 22:05:47.193: D/@gaurav(417): java.lang.NullPointerException
12-08 22:05:47.193: D/@gaurav(417): at com.gaurav.contactmanager.DataBaseHelper.addContact(DataBaseHelper.java:63)
12-08 22:05:47.193: D/@gaurav(417): at com.gaurav.contactmanager.ContactManager.onClick(ContactManager.java:95)
12-08 22:05:47.193: D/@gaurav(417): at android.view.View.performClick(View.java:2485)
12-08 22:05:47.193: D/@gaurav(417): at android.view.View$PerformClick.run(View.java:9080)
12-08 22:05:47.193: D/@gaurav(417): at android.os.Handler.handleCallback(Handler.java:587)
12-08 22:05:47.193: D/@gaurav(417): at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 22:05:47.193: D/@gaurav(417): at android.os.Looper.loop(Looper.java:123)
12-08 22:05:47.193: D/@gaurav(417): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-08 22:05:47.193: D/@gaurav(417): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 22:05:47.193: D/@gaurav(417): at java.lang.reflect.Method.invoke(Method.java:507)
12-08 22:05:47.193: D/@gaurav(417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-08 22:05:47.193: D/@gaurav(417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-08 22:05:47.193: D/@gaurav(417): at dalvik.system.NativeStart.main(Native Method)

code:
DataBaseHelper.java

package com.gaurav.contactmanager;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "contactsManager";

    // Contacts table name
    private static final String TABLE_CONTACTS = "contacts";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";

    public DataBaseHelper(Context context) {        
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.d("@gaurav", "database object created but not database...");
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {       
        Log.d("@gaurav", "database creating...");
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER ," + KEY_NAME + " TEXT," + KEY_PH_NO
                + " TEXT PRIMARY KEY" + ")"; // TEXT PRIMARY KEY
        db.execSQL(CREATE_CONTACTS_TABLE);
        Log.d(ContactManagerUtil.TAG_EXIT, ContactManagerUtil.getInstance().getMehtodName());
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("@gaurav", "database upgrading...");
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
        Log.d(ContactManagerUtil.TAG_EXIT, ContactManagerUtil.getInstance().getMehtodName());
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */
    SQLiteDatabase db;
    // Adding new contact
    void addContact(MyContact contact) {
        Log.d(ContactManagerUtil.TAG_ENTER, ContactManagerUtil.getInstance().getMehtodName());

         db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
        values.put(KEY_PH_NO, contact.getNumber()); // Contact Phone

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        Log.d("@gaurav", "Insert completed..");
        db.close(); // Closing database connection
    }

    public void onOpen(SQLiteDatabase db) {
        Log.d("@gaurav", "onOpen database");
    };
    // Getting single contact
    MyContact getContact(int id) {
        Log.d("@gaurav", "getting contact");

         db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
                KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        MyContact contact = new MyContact(
                Integer.parseInt(cursor.getString(0)), cursor.getString(1),
                cursor.getString(2));
        // return contact
        Log.d("@gaurav", "got contact, now returning ...");
        return contact;
    }

    // Getting All Contacts
    public List<MyContact> getAllContacts() {
        Log.d(ContactManagerUtil.TAG_ENTER, ContactManagerUtil.getInstance().getMehtodName());

        List<MyContact> contactList = new ArrayList<MyContact>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

         db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                MyContact contact = MyContact.getInstance();
                contact.setId(Integer.parseInt(cursor.getString(0)));
                contact.setName(cursor.getString(1));
                contact.setNumber(cursor.getString(2));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }
        Log.d(ContactManagerUtil.TAG_EXIT, ContactManagerUtil.getInstance().getMehtodName());

        // return contact list
        return contactList;
    }

    // Updating single contact
    public int updateContact(MyContact contact) {
        Log.d(ContactManagerUtil.TAG_ENTER, ContactManagerUtil.getInstance().getMehtodName());

         db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName());
        values.put(KEY_PH_NO, contact.getNumber());
        Log.d(ContactManagerUtil.TAG_EXIT, ContactManagerUtil.getInstance().getMehtodName());

        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getId()) });
    }

    // Deleting single contact
    public void deleteContact(MyContact contact) {
        Log.d(ContactManagerUtil.TAG_ENTER, ContactManagerUtil.getInstance().getMehtodName());

         db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getId()) });
        db.close();
        Log.d(ContactManagerUtil.TAG_EXIT, ContactManagerUtil.getInstance().getMehtodName());

    }

    // Getting contacts Count
    public int getContactsCount() {
        Log.d(ContactManagerUtil.TAG_ENTER, ContactManagerUtil.getInstance().getMehtodName());

        String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
         db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
        Log.d(ContactManagerUtil.TAG_EXIT, ContactManagerUtil.getInstance().getMehtodName());

        // return count
        return cursor.getCount();
    }

}

This is the method where exception is occuring, possibly because db is null because onCreate() is not invoked automatically.

Activity code from where calling is done:

 public void onClick(View v) {
        Log.d(TAG_ENTER, getMehtodName());
        switch (v.getId()) {
        case R.id.submit:
            try {
                if (contact == null) {
                    contact = MyContact.getInstance();
                }
                contact.setId(id++);
                contact.setName(name.getText().toString());
                contact.setNumber(number.getText().toString());
                if (dbHandler != null) {
                    dbHandler.addContact(contact);
                } else {
                    dbHandler = new DataBaseHelper(getApplicationContext());
                    dbHandler.addContact();
                }
            } catch (Exception ex) {
                Log.d("@gaurav", "Problem while cerating contact", ex);
                Log.wtf("@gaurav", "what ...... :)");
            } finally {
                contact.flush();
                name.setText("");
                number.setText("");
            }
            break;
        case R.id.view:
            // Intent intent = new Intent(this, ContactView.class);
            // startActivity(intent);

            break;
        default:
            break;
        }
        Log.d(TAG_EXIT, getMehtodName());
    } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:04:09+00:00Added an answer on June 15, 2026 at 3:04 pm

    this is line number 63:

    Log.d(ContactManagerUtil.TAG_ENTER, 
          ContactManagerUtil.getInstance().getMehtodName());
    

    The Logcat is stating that ContactManagerUtil.getInstance() returns null… You should start debugging inside the getInstance() method. Also you have a typo in this method’s name: getMehtodName().

    (Post ContactManagerUtil.getInstance() if you still need help.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing a problem with jquery and have a question. Please answer them
I am facing a problem with jquery and have a question. Please answer them
I am facing problem to get variable from query string. I have used htaccess
Again expecting your helps. Facing a small might be stupid problem. I am creating
I'm facing a bit of an odd problem here. I just launched: http://claudiu.phpfogapp.com/ To
I have configured Hadoop and Hive on Windows through Cygwin. But I am facing
I'm facing a hard problem: Imagine I have a map of an entire country,
I have facing problem in listview i am getting data from server and after
I'm currently facing a weird problem while executing a command from my bash script.
Iam facing problem in understanding and converting a matlab code into opencv. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.