2011-11-15 70 views
0

當我嘗試在我的Android應用程序中插入數據到我的sqlite數據庫時,我得到一個空指針異常。我看過代碼,一切似乎都是按順序進行的,我爲什麼會遇到空指針異常而感到困惑。代碼如下:Android的sqlite數據庫空指針異常

import java.sql.Blob; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 


public class DBAdapter { 

    public static final String KEY_ROWID = "id"; 
    public static final String KEY_STUDENTNAME = "studentname"; 
    public static final String KEY_DOB = "dob"; 
    public static final String KEY_ADDRESS1 = "address1"; 
    public static final String KEY_ADDRESS2 = "address2"; 
    public static final String KEY_TOWN = "town"; 
    public static final String KEY_POSTCODE = "postcode"; 
    public static final String KEY_PHONE = "phone"; 
    public static final String KEY_STUDENT_PIC = "studentpic"; 

    private static final String TAG = "DBAdapter"; 



    private static final String DATABASE_NAME = "studentDB3"; 
    private static final String DATABASE_TABLE = "tblstudents"; 
    private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE = 
     "create table tblstudents (id integer primary key autoincrement, " 
     + "studentname text not null, dob text not null, " 
     + "address1 text not null, address2 text not null, " 
     + "town text not null, postcode text not null, " 
     + " phone text not null, studentpic blob);"; 

    private final Context context; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL(DATABASE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, 
     int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion 
        + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS tblStudents"); 
      onCreate(db); 
     } 
    }  

    //---opens the database--- 
    public DBAdapter open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    //---closes the database---  
    public void close() 
    { 
     DBHelper.close(); 
    } 

    //---insert a title into the database--- 
    public long insertStudent(String studentname, String dob, String address1, String address2, String town, String postcode, String phone, byte[] studentpic) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_STUDENTNAME, studentname); 
     initialValues.put(KEY_DOB, dob); 
     initialValues.put(KEY_ADDRESS1, address1); 
     initialValues.put(KEY_ADDRESS2, address2); 
     initialValues.put(KEY_TOWN, town); 
     initialValues.put(KEY_POSTCODE, postcode); 
     initialValues.put(KEY_PHONE, phone); 
     initialValues.put(KEY_STUDENT_PIC, studentpic); 

     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    //---deletes a particular title--- 
    public boolean deleteTitle(long rowId) 
    { 
     return db.delete(DATABASE_TABLE, KEY_ROWID + 
       "=" + rowId, null) > 0; 
    } 

    //---retrieves all the titles--- 
    public Cursor getAllStudents() 
    { 
     return db.query(DATABASE_TABLE, new String[] { 
       KEY_ROWID, 
       KEY_STUDENTNAME, 
       KEY_DOB, 
       KEY_ADDRESS1, 
       KEY_ADDRESS2, 
       KEY_TOWN, 
       KEY_POSTCODE, 
       KEY_PHONE, 
       KEY_STUDENT_PIC 
          }, 
       null, 
       null, 
       null, 
       null, 
       null); 
    } 

    //---retrieves a particular title--- 
    public Cursor getStudent(long rowId) throws SQLException 
    { 
     Cursor mCursor = 
       db.query(true, DATABASE_TABLE, new String[] { 
         KEY_ROWID, 
         KEY_STUDENTNAME, 
         KEY_DOB, 
         KEY_ADDRESS1, 
         KEY_ADDRESS2, 
         KEY_TOWN, 
         KEY_POSTCODE, 
         KEY_PHONE, 
         KEY_STUDENT_PIC 
         }, 
         KEY_ROWID + "=" + rowId, 
         null, 
         null, 
         null, 
         null, 
         null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    //---updates a student--- 
    public boolean updateStudent(long rowId, String studentname, 
    String dob, String address1, String address2, String town, 
    String postcode, String phone, byte[] studentpic) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_STUDENTNAME, studentname); 
     args.put(KEY_DOB, dob); 
     args.put(KEY_ADDRESS1, address1); 
     args.put(KEY_ADDRESS2, address2); 
     args.put(KEY_TOWN, town); 
     args.put(KEY_POSTCODE, postcode); 
     args.put(KEY_PHONE, phone); 
     args.put(KEY_STUDENT_PIC, studentpic); 

     return db.update(DATABASE_TABLE, args, 
         KEY_ROWID + "=" + rowId, null) > 0; 
    } 




} 

+2

你有什麼特別之處?它會告訴我們很多關於它的原因。 – Malcolm

+2

在這裏複製併發布你的logcat。從DDMS的角度來看。 Eclipse> Window> show View> other> DDMS' –

回答

1

在Android中,ID列應該是_id,不id爲您設置:

public static final String KEY_ROWID = "id"; 

這可能是值得考慮的。