代码之家  ›  专栏  ›  技术社区  ›  keser

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

  •  -1
  • keser  · 技术社区  · 6 年前

    在插入androidsqlite数据库时,我想跳过用户没有填充的值。 但我得到空指针异常。

    在我的DBHelper类中,我尝试了两个“。。。不空”和没有不空选项,但我认为这不是解决办法。

     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:Stacktrace。

    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)
    

         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 回复  |  直到 6 年前
        1
  •  1
  •   SamHoque    6 年前

    嗨,例如,你必须检查它们是否为空

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

    o 如果为空,则将dateStr设置为空,否则将使用o。你可以把它用于所有的变量。