sean
sean

Reputation: 133

SQLite Database Error [FATAL EXCEPTION error ]

mp application crashes upon launch. I receive a " java.lang.RuntimeException: Unable to get provider" error. Im assuming this has to do with my sqlite DB since My contactprovider now hold the code to create the DB. Any help figuring the caus eof this woul dbe appriciated. My Logcat, DB Class, and provicer class are below.

logcat:

 04-24 12:59:07.305: E/AndroidRuntime(1482): FATAL EXCEPTION: main
 04-24 12:59:07.305: E/AndroidRuntime(1482): java.lang.RuntimeException: Unable to get provider com.loginplus.home.ListProvider: java.lang.NullPointerException
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.app.ActivityThread.installProvider(ActivityThread.java:4240)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.app.ActivityThread.installContentProviders(ActivityThread.java:3992)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3946)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.app.ActivityThread.access$1300(ActivityThread.java:123)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.os.Handler.dispatchMessage(Handler.java:99)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.os.Looper.loop(Looper.java:137)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.app.ActivityThread.main(ActivityThread.java:4424)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at java.lang.reflect.Method.invokeNative(Native Method)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at java.lang.reflect.Method.invoke(Method.java:511)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at dalvik.system.NativeStart.main(Native Method)
 04-24 12:59:07.305: E/AndroidRuntime(1482): Caused by: java.lang.NullPointerException
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.database.DatabaseUtils.getSqlStatementType(DatabaseUtils.java:1318)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1893)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at com.loginplus.home.dataStore.onCreate(dataStore.java:27)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at com.loginplus.home.ListProvider.onCreate(ListProvider.java:75)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.content.ContentProvider.attachInfo(ContentProvider.java:944)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    at android.app.ActivityThread.installProvider(ActivityThread.java:4237)
 04-24 12:59:07.305: E/AndroidRuntime(1482):    ... 12 more

DB Class:

 public class dataStore extends SQLiteOpenHelper {

//Table attributes
private static final String DATABASE_NAME = "SiteLogindb";
private static  String DSTORE_CREATE;
private static final int DATABASE_VERSION = 2;


dataStore(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(DSTORE_CREATE);

    }           
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        if(oldVersion == 1 && newVersion == 2){
            //Upgrade the database  
    }

        }

            }

Content Provider:

   public class ListProvider extends ContentProvider {


// Content Provider Uri and Authority
private static String AUTHORITY = "com.loginplus.home.ListProvider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/infotable" );

// Used for searching words or looking up a single definition
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
        + "/mt-tutorial";
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
        + "/mt-tutorial";

//Database Columns
public static final String COLUMN_ROWID = "_id";
public static final String COLUMN_NAME_SITE = "sName";
public static final String COLUMN_NAME_ADDRESS = "wUrl";
public static final String COLUMN_NAME_USERNAME = "uName";
public static final String COLUMN_NAME_PASSWORD = "pWord";
public static final String COLUMN_NAME_NOTES = "lNotes";

// Database related Constants
public static final String DATABASE_NAME = "SiteLogindb";
public static final int DATABASE_VERSION = 2;
public static final String TABLE_NAME_INFOTABLE = "infoTable";

public static final String DSTORE_CREATE = "create table if not exists " +
        TABLE_NAME_INFOTABLE + " ("+ COLUMN_ROWID + " integer primary key autoincrement,"

                    + COLUMN_NAME_SITE + " text not null,"
                    + COLUMN_NAME_ADDRESS + " text not null,"
                    + COLUMN_NAME_USERNAME + " text not null,"
                    + COLUMN_NAME_PASSWORD + " text not null,"
                    + COLUMN_NAME_NOTES + " text not null);";

//UriMatcher stuff
private static final int LIST = 1;
private static final int LIST_ID = 2;
private static final UriMatcher sURIMatcher = buildUriMatcher();

private SQLiteDatabase lDB;


private static UriMatcher buildUriMatcher() {

    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    // get definitions
    matcher.addURI(AUTHORITY, "infoTable", LIST);
    matcher.addURI(AUTHORITY, "infoTable/#", LIST_ID);
    return matcher;

}

@Override
public boolean onCreate() {
    lDB = new dataStore(getContext()).getWritableDatabase();
    return true;
}

 @Override
public Cursor query(Uri uri, String[] ignored1, String ignored2,
        String[] ignored3, String ignored4) {

    String[] projection = new String[] {

            ListProvider.COLUMN_ROWID,
            ListProvider.COLUMN_NAME_SITE,
            ListProvider.COLUMN_NAME_ADDRESS,
            ListProvider.COLUMN_NAME_USERNAME,
            ListProvider.COLUMN_NAME_PASSWORD,
            ListProvider.COLUMN_NAME_NOTES };

    Cursor c;
    switch (sURIMatcher.match(uri)) {

    case LIST:
        c = lDB.query(ListProvider.TABLE_NAME_INFOTABLE, projection, null, null, null, null, null);
        break;

    case LIST_ID:
        c = lDB.query(ListProvider.TABLE_NAME_INFOTABLE, projection, ListProvider.COLUMN_ROWID + "=?", 
                new String[] { Long.toString(ContentUris.parseId(uri)) },
                    null, null, null, null);

       if (c !=null && c.getCount() > 0) {
           c.moveToFirst();
       }
        break;

    default:
        throw new IllegalArgumentException("Unknown URI");
    }

    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
  }

   //Inserting DB entries
    @Override
    public Uri insert(Uri uri, ContentValues values) {

        values.remove(ListProvider.COLUMN_ROWID);
        long id = lDB.insertOrThrow(TABLE_NAME_INFOTABLE, null, values);
        getContext().getContentResolver().notifyChange(uri, null);
        return ContentUris.withAppendedId(uri, id);
    }
//Deleting DB entries
@Override
public int delete(Uri uri, String ignored1, String[] ignored2) {
    int count = lDB.delete(ListProvider.TABLE_NAME_INFOTABLE, ListProvider.COLUMN_ROWID + "=?", 
    new String[] { Long.toString(ContentUris.parseId(uri)) });

    if (count > 0)
        getContext().getContentResolver().notifyChange(uri, null);
    return count;

}


//updating DB entries
@Override
public int update(Uri uri, ContentValues values, String ignored1,
        String[] ignored2) {
    int count = lDB.update(TABLE_NAME_INFOTABLE, values, COLUMN_ROWID + "=?",new String[] {
            Long.toString(ContentUris.parseId(uri)) });
    if( count>0)
        getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

@Override
public String getType(Uri uri) {
    // TODO Auto-generated method stub
    switch (sURIMatcher.match(uri)) {

    case LIST:
        return CONTENT_ITEM_TYPE;
    case LIST_ID:
        return CONTENT_TYPE;
    default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);  
    }
}
  }

Upvotes: 0

Views: 445

Answers (2)

Shobhit Puri
Shobhit Puri

Reputation: 26007

Possible explanation: I can't see where you've initialize DSTORE_CREATE string before you call it in db.execSQL(DSTORE_CREATE) inside onCreate() ? You've defined it in your content provider but dataStore object does not come to know about this declaration that you've made in your content provide class.

Possible solution: If you simply want to get rid of it, define the DSTORE_CREATE into the dataStore class instead of the ListProvider class.

Hope this helps. If still you have issues, please comment.

Upvotes: 2

Cristian
Cristian

Reputation: 200080

You have to initialize DSTORE_CREATE in the dataStore class or use the one in ListProvider.

Upvotes: 1

Related Questions