dimanche 13 décembre 2015

getting null exception in adding data from textview to sqlite database android

i am so newbie in sqlite so please if you could, please help.

btnsubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            final String placeID = placeidtext.getText().toString();
            final String placename = placeName.getText().toString();
            final String address = addressHere.getText().toString();

            favDB.addFavorites(new FavoritesListItem(userId, placeID, placename, address));
            Toast.makeText(getApplicationContext(),
                    "Added to Favorites\n"+placeID+"\n"+placename+"\n"+address+"\n", Toast.LENGTH_SHORT)
                    .show();

        }

    });

here is my database

package com.fpzlabs.shakedatapp;


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

     public class FavoritesDB extends SQLiteOpenHelper {

String placeName;
String AddresS;
String PlaceID;

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

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

// Contacts table name
private static final String TABLE_FAVORITES = "favoritestbl";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_FAVORITE_PLACE_NAME = "name";
private static final String KEY_ADDRESS = "address";
private static final String KEY_PLACE_ID = "place_id";

public FavoritesDB(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}



// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_FAVORITES + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_FAVORITE_PLACE_NAME + " TEXT,"
            + KEY_PLACE_ID + " TEXT,"
            + KEY_ADDRESS + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);


}





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

    // Create tables again
    onCreate(db);
}

public void addFavorites(FavoritesListItem favDB)
{

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_FAVORITE_PLACE_NAME, favDB.getPlaceName());
    contentValues.put(KEY_ADDRESS, favDB.getAddress());
    contentValues.put(KEY_PLACE_ID, favDB.getPlaceID());


    Log.d("get USER", "NAME = " + favDB.getPlaceName());
    Log.d("get USER", "PH NO = " + favDB.getAddress());
    Log.d("get USER", "ADDRESS = " + favDB.getPlaceID());
    Log.d("values", "values = " + contentValues.toString());

    db.insert(TABLE_FAVORITES, null, contentValues);
    db.close(); // Closing database connection


}




public void addRow(SQLiteDatabase theDb) {
    // Setup the ContentValues object
    ContentValues values = new ContentValues();
    //.. add stuff to values
    // insert in to the database
    theDb.insert(TABLE_FAVORITES, null, values);
}


  }

the strings are from textviews and i wanted to add them into the database but nullpointer exception shows up.

i dont know but stackoverflow needs me to add more details in my question yet i dont know what to say..

Aucun commentaire:

Enregistrer un commentaire