在插入时获得Null指针异常:Sqllite& Android的

时间:2016-06-22 06:18:59

标签: android sqlite null sql-insert

我在插入上得到空指针异常,我无法弄清楚原因。能否请你帮忙? 代码 -

public class DataBaseHelper extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "recorddb";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_NAME_0 = "MONTH";
    public static final String COLUMN_NAME_1 = "ERYTHROCYTE";
    public static final String COLUMN_NAME_2 = "HAEMOGLOBIN";
    public static final String COLUMN_NAME_3 = "PCV";
    public static final String COLUMN_NAME_4 = "RDW";
    public static final String COLUMN_NAME_5 = "MCV";
    public static final String COLUMN_NAME_6 = "MCH";
    public static final String COLUMN_NAME_7 = "MCHC";
    public static final String COLUMN_NAME_8 = "LEUCOCYTECOUNT";
    public static final String COLUMN_NAME_9 = "PLATELETS";
    public static final String COLUMN_NAME_10 = "NEUTROPHILS";
    public static final String COLUMN_NAME_11 = "EOSINOPHILS";
    public static final String COLUMN_NAME_12 = "LYMPHOCYTES";
    public static String CURRENT_MONTH = "CURRENT_MONTH";
    Cursor cursor = null;

    public DataBaseHelper(Context context) {
        super(context, "recorddb", null, 2);
    }

    /* Start of onCreate() */
    @Override
    public void onCreate(SQLiteDatabase db) {
         db = this.getWritableDatabase();
        try {
            db.execSQL("CREATE TABLE IF NOT EXISTS recorddb (id INTEGER PRIMARY KEY AUTOINCREMENT, MONTH TEXT, ERYTHROCYTE TEXT, HAEMOGLOBIN TEXT, PCV TEXT, RDW TEXT, MCV TEXT, MCH TEXT, MCHC TEXT,LEUCOCYTECOUNT TEXT, PLATELETS TEXT, NEUTROPHILS  TEXT, EOSINOPHILS TEXT,LYMPHOCYTES TEXT);");
            //db.execSQL("CREATE TABLE IF NOT EXISTS recorddb (id INTEGER PRIMARY KEY AUTOINCREMENT, MONTH TEXT, ERYTHROCYTE FLOAT, HAEMOGLOBIN FLOAT, PCV FLOAT, RDW FLOAT, MCV FLOAT, MCH FLOAT, MCHC FLOAT,LEUCOCYTECOUNT FLOAT, PLATELETS FLOAT, NEUTROPHILS FLOAT, EOSINOPHILS FLOAT,LYMPHOCYTES FLOAT);");
        } catch (SQLiteException sqlex) {
            Log.e("Logger:DBH:", "onCreate:SQLException::" + sqlex);
        } catch (Exception ex) {
            Log.e("Logger:DBH", "onCreate:SQL Exception:" + ex);
        } finally {
            db.close();     // Close Database Transaction
        }
        Log.e("Logger:DBH", "onCreate:Table created");
    }

    /* Start of onUpgrade() */
    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }


    /* Start of InsertRecord() */
    public boolean InsertRecord(String MONTH, Float ERYTHROCYTE, Float HAEMOGLOBIN, Float PCV, Float RDW, Float MCV, Float MCH, Float MCHC, Float LEUCOCYTECOUNT, Float PLATELETS, Float NEUTROPHILS, Float EOSINOPHILS, Float LYMPHOCYTES) {

        Log.e("Logger:DBH", "InsertRecord:Enter");
        SQLiteDatabase db = getWritableDatabase();     // Get the Reference of the Database
        Log.e("Logger:DBH", "InsertRecord:ERYTHROCYTE" + ERYTHROCYTE);
        //duplicateMonthCheck
        if (duplicateMonthCheck(MONTH)) {
            Log.e("Logger:DBH", "InsertRecord:Duplicate Exists" + MONTH);
            //UtilityHelper.showMessage("Alert","Record for " + strDate + " already exists. Correct the month/year & try adding again", "Ok",new AddActivity());
            return false;
        } else {

            try {
                Log.e("Logger:DBH", "InsertRecord:Duplicate Not Exists" + MONTH);
                ContentValues values = new ContentValues();
                values.put("MONTH", MONTH);
                values.put("ERYTHROCYTE", ERYTHROCYTE);
                values.put("HAEMOGLOBIN", HAEMOGLOBIN);
                values.put("PCV", PCV);
                values.put("RDW", RDW);
                values.put("MCV", MCV);
                values.put("MCH", MCH);
                values.put("MCHC", MCHC);
                values.put("LEUCOCYTECOUNT", LEUCOCYTECOUNT);
                values.put("PLATELETS", PLATELETS);
                values.put("NEUTROPHILS", NEUTROPHILS);
                values.put("EOSINOPHILS", EOSINOPHILS);
                values.put("LYMPHOCYTES", LYMPHOCYTES);

                Log.e("Logger:DBH", "InsertRecord Data >>" +MONTH+","+ ERYTHROCYTE+","+HAEMOGLOBIN+","+PCV+","+RDW+","+MCV+","+MCH+","+MCHC+","+LEUCOCYTECOUNT+","+PLATELETS+","+NEUTROPHILS+","+EOSINOPHILS+","+LYMPHOCYTES+" :");
                Log.e("Logger:DBH", "InsertRecord Month ..................");
                Log.e("Logger:DBH", "InsertRecord>>values.toString:: " + values.toString());
                Log.e("Logger:DBH", "InsertRecord Month .................."+db.getAttachedDbs());
                if (db.insert("recorddb", null, values) != -1)
                    return true;

            } catch (SQLiteException sqlex) {
                Log.e("Logger:DBH:", "InsertRecord:SQLException::" + sqlex+",ST :"+sqlex.getStackTrace());
            } catch (Exception ex) {
                Log.e("Logger:DBH", "InsertRecord: Exception:::" + ex +",toStr :"+ex);
                Log.e("Logger:DBH", "InsertRecord: Exception:::" + ex +",LocMsg :"+ex.initCause(null));
                Log.e("Logger:DBH", "InsertRecord: Exception:::" + ex +",ST :"+ex.getStackTrace());
            } finally {
                db.close();     // Close Database Transaction
            }
            Log.e("Logger:DBH", "InsertRecord:Exit");
            return false;
        }
    }

    /* End of InsertRecord() */

    /* Start of duplicateMonthCheck()*/
    public boolean duplicateMonthCheck(String month) {

        Log.e("Logger:DBH", "duplicateMonthCheck:Enter");
        ArrayList recByMonth = new ArrayList<>();
        recByMonth = getRecordsByMonth(month);
        if (recByMonth != null && !recByMonth.isEmpty()) {
            Log.e("Logger:DBH", "duplicateMonthCheck:Yes duplicates");
            return true;
        } else {
            Log.e("Logger:DBH", "duplicateMonthCheck: No duplicates");
            return false;
        }

    }
    /* End of duplicateMonthCheck()*/

----删除了类的剩余函数-----------

记录:

06-22 11:34:19.760 30764-30764/com.ajay.projects.activity E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.ajay.projects.models.UtilityHelper.access$super
 E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.ajay.projects.models.UtilityHelper.access$super
 E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.ajay.projects.models.UtilityHelper.access$super
 E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.ajay.projects.models.UtilityHelper.access$super
 E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.ajay.projects.models.UtilityHelper.access$super
 E/Logger:Utility: getCurrentMonthList() Enter
 E/Logger:Utility: getCurrentMonthList() Exit[Ljava.lang.String;@412be7f8
 E/Logger:Utility: getCurrentYear() Enter
 E/Logger:Utility: getCurrentYear() Exit[Ljava.lang.String;@412bf200
 E/Logger:AddActivity:: OnCreate:Enter
 E/Logger:DBH: getMonthList:Enter
 E/Logger:DBH: SELECT MONTH FROM recorddb1
 E/Logger:DBH: SELECT MONTH FROM recorddb : xNewData to Array[Ljava.lang.Object;@412e9510
 E/Logger:DBH: SELECT MONTH FROM recorddb : xNewData to Array[Ljava.lang.Object;@412e9818
 E/Logger:DBH: getMonthList:Exit
 E/Logger:Base: dataExists True
 E/Logger:AddActivity:: OnCreate:nef GetINflator
 E/Logger:AddActivity:: OnCreate:Enter2
 E/Logger:AddActivity:: OnCreate:Enter3
 E/Logger:AddActivity: setMonthSpinner:Enter
 E/Logger:AddActivity: setMonthSpinner:Exit
 E/Logger:AddActivity: setYearSpinner:Enter
 E/Logger:AddActivity: setYearSpinner:Exit
 E/Logger:AddActivity:: OnCreate:After Spinner1
 E/Logger:AddActivity:: OnCreate:Exit
  E/Logger:AddActivity: onClick:isNotEmpty
  E/Logger:AddActivity: btnAdd:OnClick-SelectedMonth>>Feb-2016
  E/Logger:AddActivity: btnAdd:getText().toString:Feb-2016','2','2','2','2','2','2','2','2','22','2','2','2
  E/Logger:AddActivity: btnAdd:getText():Feb-2016','2','2','2','2','2','2','2','2','22','2','2','2
  E/Logger:DBH: InsertRecord:Enter
  E/Logger:DBH: InsertRecord:ERYTHROCYTE2.0
  E/Logger:DBH: duplicateMonthCheck:Enter
  E/Logger:DBH:: getRecordsByMonth:EnterFeb-2016
  E/Logger:DBH:: getRecordsByMonth:Exit
  E/Logger:DBH: duplicateMonthCheck: No duplicates
  E/Logger:DBH: InsertRecord:Duplicate Not ExistsFeb-2016
  E/Logger:DBH: InsertRecord Data >>Feb-2016,2.0,2.0,2.0,2.0,2.0,2.0,2.0,0.002,0.22,2.0,2.0,2.0 :
  E/Logger:DBH: InsertRecord Month ..................
  E/Logger:DBH: InsertRecord>>values.toString:: LYMPHOCYTES=2.0 NEUTROPHILS=2.0 LEUCOCYTECOUNT=0.002 MCH=2.0 PCV=2.0 PLATELETS=0.22 HAEMOGLOBIN=2.0 MCHC=2.0 MCV=2.0 RDW=2.0 MONTH=Feb-2016 EOSINOPHILS=2.0 ERYTHROCYTE=2.0
  E/Logger:DBH: InsertRecord Month ..................null
  E/Logger:DBH: InsertRecord: Exception:::java.lang.NullPointerException,toStr :java.lang.NullPointerException
  E/Logger:DBH: InsertRecord: Exception:::java.lang.NullPointerException,LocMsg :java.lang.NullPointerException
  E/Logger:DBH: InsertRecord: Exception:::java.lang.NullPointerException,ST :[Ljava.lang.StackTraceElement;@4139d8e8
  E/Logger:DBH: InsertRecord:Exit
  E/Logger:Util: ShowMessage : Enter
  E/Logger:Util: ShowMessage : Details-AlertRecord for Feb-2016 already exists. Correct the month/year & try adding againOkcom.ajay.projects.activity.AddActivity@412b3668
  E/Logger:Util: ShowMessage : Exit

---------------------日志结束-----------------

0 个答案:

没有答案