在将textview中的数据添加到sqlite数据库android时获取null异常

时间:2015-12-13 16:37:33

标签: java android sqlite

我是sqlite的新手,所以如果可以,请帮助。

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();

        }
    });

这是我的数据库

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);
}
  }

这是我的logcat

12-14 00:58:54.810 23314-23314/com.blogspot.hongthaiit.viewpagerwithtabs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.blogspot.hongthaiit.viewpagerwithtabs, PID: 23314
java.lang.NullPointerException
at com.fpzlabs.shakedatapp.Coffee$5.onClick(Coffee.java:340)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19270)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

字符串来自textviews,我想将它们添加到数据库中,但是会显示nullpointer异常。

我不知道但是stackoverflow需要我在我的问题中添加更多细节但我不知道该说些什么..

0 个答案:

没有答案