0

This code was occur NullPointException. I think onCreate method is not called so 'db' in null. But i cant found wrong spot.ease look at the code below and give me any suggestions. Any help will be appreciated.

public class DBManager {
    private static DBManager instance;

    public static DBManager getInstance() {
        if (instance == null) {
            instance = new DBManager();
        }
        return instance;
    }

    DBHelper openHelper;

    private DBManager() {
        openHelper = new DBHelper(MyApplication.getContext());
    }

    public void insertPerson(PersonData person) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(PersonTable.NAME, person.name);
        values.put(PersonTable.COMMENT, person.comment);
        values.put(PersonTable.PHONE_NUM,person.phoneNum);
        values.put(PersonTable.TYPE,person.type);
        values.put(PersonTable.PROFILE_IMG,person.resId);

        db.insert(PersonTable.TABLE_NAME, null, values);
        db.close();
    }

public class DBHelper extends SQLiteOpenHelper {

        private final static String DB_NAME = "kakao.db";
        private final static int DB_VERSION = 1;

        public DBHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql = "CREATE TABLE "+ PersonTable.TABLE_NAME + "("
                    + PersonTable._ID
                    + " integer PRIMARY KEY autoincrement , "
                    + PersonTable.NAME + " text, "
                    + PersonTable.PHONE_NUM + " text, "
                    + PersonTable.PROFILE_IMG + " text, "
                    + PersonTable.COMMENT + " text, "
                    + PersonTable.TYPE + " text);";
            db.execSQL(sql);
        }

line 29 is 'SQLiteDatabase db = openHelper.getWritableDatabase();'

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at taca.yongseong.kakaomessanger.DBManager.insertPerson(DBManager.java:29)
at taca.yongseong.kakaomessanger.Friend.getContact(Friend.java:137)
at taca.yongseong.kakaomessanger.Friend.setData(Friend.java:106)
at taca.yongseong.kakaomessanger.Friend.onCreateView(Friend.java:51)
2
  • Can you post the logcat with exception? Commented Apr 17, 2014 at 4:04
  • @Szymon Im add logcat in post. Commented Apr 17, 2014 at 4:29

1 Answer 1

1

The Context you passed to the SQLiteOpenHelper constructor was null.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.