如果SQLite中的值为null,则跳过插入

时间:2018-10-11 14:16:30

标签: java android sqlite

在插入Android SQLite数据库时,我想跳过用户未填写的值。  但是我得到了空指针异常。

在我的DBHelper类中,我同时尝试了“ ... NOT NULL”和非null选项,但我认为这不是解决方案。

 public void addToDb(View view) {
    SharedPreferences sharedPreferences = getSharedPreferences("entry",0);
    float  lastlatitude = sharedPreferences.getFloat("latitude",0f);
    float lastlongitude =  sharedPreferences.getFloat("longitude",0f);
    if (mTitleInput.getText().length() == 0 ||
            mEntryInput.getText().length() == 0) {
        return;
    }
    try {
        Date a = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, ''yy");
        String o = sdf.format(a);

        title = mTitleInput.getText().toString();
        entry = mEntryInput.getText().toString();
        dateStr = o;
        image = getApplicationContext().getFilesDir().getAbsolutePath() + String.valueOf(System.currentTimeMillis()) + ".jpg";
        rating = mRatingBar.getRating();
        latitude = lastlatitude;
        longitude = lastlongitude;

    } catch (NumberFormatException ex) {
        Log.e(LOG_TAG, "Failed to parse party size text to number: " + ex.getMessage());
    }

    // Add to mDb
    addNewEntry(title, entry, dateStr, image, rating, latitude, longitude);



    //clear UI text fields
    mTitleInput.getText().clear();
    mEntryInput.getText().clear();
}

编辑1:堆栈跟踪。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aok.secretdiarywithlock, PID: 29148
java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference
    at com.aok.secretdiarywithlock.CreateEntry.addNewEntry(CreateEntry.java:157)
    at com.aok.secretdiarywithlock.CreateEntry.addToDb(CreateEntry.java:139)
    at com.aok.secretdiarywithlock.CreateEntry$1.onClick(CreateEntry.java:93)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

编辑2:错误代码

     private long addNewEntry(String title, String entry, String date, String image, Float rate, Float latitude, Float longitude){
        ContentValues cv = new ContentValues();
        cv.put(EntryContract.EntryEntry.COLUMN_TITLE,title);
        cv.put(EntryContract.EntryEntry.COLUMN_TEXT,entry);
        cv.put(EntryContract.EntryEntry.COLUMN_DATE,date);
        cv.put(EntryContract.EntryEntry.COLUMN_IMAGE,image);
        cv.put(EntryContract.EntryEntry.COLUMN_TEXT,entry);
        cv.put(EntryContract.EntryEntry.COLUMN_RATING,rate);
        cv.put(EntryContract.EntryEntry.COLUMN_LOCATION_LAT,latitude);
        cv.put(EntryContract.EntryEntry.COLUMN_LOCATION_LONG,longitude);
//        Next line is throwing error
        return mDb.insert(EntryContract.EntryEntry.TABLE_NAME, null, cv);
    }

1 个答案:

答案 0 :(得分:1)

嗨,您必须检查它们是否为空

dateStr = o == null ? "" : o;

这将检查o是否为null,如果将其设置为dateStr为空,否则将使用o。您可以将其用于所有变量。