vendredi 26 février 2016

Android SQLite java.lang.NullPointerException insertWithOnConflict

SQLite insertion crashing. I wrote code to restrict inserting duplicate entries in a column of table. The SQLite crashing the application, after changing the code to restrict duplicate entry of insertion. The first value insertion is crashing. After that the code was working fine to restrict the duplicate values. Trying to insert the values, that line crashing the application.

int addVehicle(VehicleGetSetter contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    //values.put(KEY_ID, contact.getID());                      // Get ID
    values.put(KEY_COLOR, contact.getColor());                  // Get Color
    values.put(KEY_VEH_CATEGORY, contact.getCategory());
    values.put(KEY_NUMBER,contact.getNumber());                 // Get Number
    values.put(KEY_MODEL, contact.getModelName());              // Get Model
    values.put(KEY_SERVICE_DATE, contact.getSeriveDate());      // Get Service Date
    values.put(KEY_PURCHASE_DATE,contact.getPurchaseDate());    // Get Purchase date
    values.put(KEY_REPAIR_PENDING, contact.getPendingRepair()); // Get Pending Repairs
    values.put(KEY_PAST_ISSUES, contact.getPastIssues());       // Get Past Issues
    values.put(KEY_CREATED_DATE, contact.getCreatedDate());     // Get Created Date
    values.put(KEY_VEHICLE_STATUS, contact.getStatus());        // Get Vehicle Status
    values.put(KEY_VEHICLE_VEND_ID, contact.getVendorId());     // Get Vendor ID
    values.put(KEY_VEHICLE_UPLOAD_STATUS, contact.getUploadState());

    //Before code to insert
    //db.insert(TABLE_RIDE_VEHICLE, null, values);

    // After changing code 
    *String number=contact.getNumber();
    if(getVehiclesCountByNumber(number)>0) {
        Log.d("Count: ", getVehiclesCountByNumber(number) + "");
        db.close(); // Closing database connection
        return 0;
    }
    else {
        //db.insertWithOnConflict(TABLE_RIDE_VEHICLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
        **db.insert(TABLE_RIDE_VEHICLE, null, values);**
        db.close(); // Closing database connection
        return 1;
    }*
}

/** Getting vehicles Count*/
public int getVehiclesCountByNumber(String veh_no) {
    String countQuery = "SELECT  * FROM " + TABLE_RIDE_VEHICLE +" WHERE "+KEY_NUMBER+" = '"+veh_no+"'";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int temp = cursor.getCount();
    Log.d("Count of vehicle : ", ""+cursor.getCount());
    cursor.close();
    db.close();
    return temp;
}

Error Log

02-27 12:04:29.530 21683-21683/helix.ridioandroidstudio E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      java.lang.NullPointerException
                                                                          at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)
                                                                          at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:115)
                                                                          at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
                                                                          at helix.ridioandroidstudio.VehicleDatabaseHandler.addVehicle(VehicleDatabaseHandler.java:106)
                                                                          at helix.ridioandroidstudio.Ridiologin$1.onClick(Ridiologin.java:81)
                                                                          at android.view.View.performClick(View.java:3511)
                                                                          at android.view.View$PerformClick.run(View.java:14109)
                                                                          at android.os.Handler.handleCallback(Handler.java:605)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                          at android.os.Looper.loop(Looper.java:137)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                                          at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire