2013-04-20 60 views
0

當我試圖向數據庫中插入一行時,我得到一個NullPointerException。我檢查了很多類似的問題,並嘗試了很多東西,但我不明白問題所在。Android SQLite數據庫insert()方法導致NullPointerException

我得到了NullPointerException放在這裏getDatabase().insert(TABLE_NAME_BUSSES, null, values);

我使用的數據庫類這樣new MyDatabase(context).addOrUpdate(bus);

Bus類只是只有getter/setter方法模型類,我在檢查值調試,我沒有訪問任何空值。

這裏是我的數據庫類:

public class MyDatabase implements IDatabaseOperations<Bus> 
{ 
    public static final String KEY_BUSSES_NUMBER = "busNumber"; 
    public static final String KEY_BUSSES_SOURCE = "busSource"; 
    public static final String KEY_BUSSES_DESTINATION = "busDestination"; 
    public static final String KEY_BUSSES_ROUTE = "busRoute"; 
    public static final String KEY_BUSSES_ISSTARRED = "busIsStarred"; 
    public static final String KEY_BUSSES_TIMESH = "busTimesH"; 
    public static final String KEY_BUSSES_TIMESC = "busTimesC"; 
    public static final String KEY_BUSSES_TIMESP = "busTimesP"; 

    private static final String DATABASE_NAME = "database"; 
    private static final int DATABASE_VERSION = 1; 

    private static final String TABLE_NAME_BUSSES = "busses"; 

    private static final String CREATE_SQL = "CREATE TABLE " + TABLE_NAME_BUSSES + " (" 
              + KEY_BUSSES_NUMBER + " INTEGER PRIMARY KEY NOT NULL, " 
              + KEY_BUSSES_SOURCE + " TEXT NOT NULL, " 
              + KEY_BUSSES_DESTINATION + " TEXT NOT NULL, " 
              + KEY_BUSSES_ROUTE + " TEXT, " 
              + KEY_BUSSES_ISSTARRED + " TEXT NOT NULL, " 
              + KEY_BUSSES_TIMESH + " TEXT, " 
              + KEY_BUSSES_TIMESC + " TEXT, " 
              + KEY_BUSSES_TIMESP + " TEXT);"; 

    private SQLiteDatabase myDatabase; 

    protected boolean isOpened = false; 

    private MyDatabaseHelper myHelper; 

    public static final String LOG_TAG = "Database"; 

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

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

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     { 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_BUSSES); 
      onCreate(db); 
     } 
    } 

    public MyDatabase(Context context) 
    { 
     myHelper = new MyDatabaseHelper(context); 
    } 

    public MyDatabase openDB() throws SQLException 
    { 
     isOpened = true; 

     myDatabase = myHelper.getWritableDatabase(); 

     return this; 
    } 

    public void closeDB() 
    { 
     isOpened = false; 

     myHelper.close(); 
    } 

    public SQLiteDatabase getDatabase() 
    { 
     return myDatabase; 
    } 

    @Override 
    public boolean addOrUpdate(Bus entry) 
    { 
     /* Result flag */ 
     boolean result = true; 

     /* If the database is not opened, open it */ 
     if(!isOpened) 
     { 
      openDB(); 
     } 

     /* Set the row */ 
     ContentValues values = new ContentValues(); 
     values.put(KEY_BUSSES_NUMBER, entry.getNumber()); 
     values.put(KEY_BUSSES_SOURCE, entry.getSource()); 
     values.put(KEY_BUSSES_DESTINATION, entry.getDestination()); 
     values.put(KEY_BUSSES_ROUTE, (entry.getRoute() != null ? entry.getRoute() : "")); 
     values.put(KEY_BUSSES_ISSTARRED, (entry.isStarred() ? 1 : 0)); 
     values.put(KEY_BUSSES_TIMESH, (entry.getTimesH() != null ? new Gson().toJson(entry.getTimesH()) : "")); 
     values.put(KEY_BUSSES_TIMESC, (entry.getTimesC() != null ? new Gson().toJson(entry.getTimesC()) : "")); 
     values.put(KEY_BUSSES_TIMESP, (entry.getTimesP() != null ? new Gson().toJson(entry.getTimesP()) : "")); 

     try 
     { 
      /* Check if the entry is already in the database */ 
      int temp = get(entry); 
      if(temp != -1 && temp == entry.getNumber()) 
      { 
       /* Updating */ 
       getDatabase().update(TABLE_NAME_BUSSES, values, KEY_BUSSES_NUMBER + "=" + entry.getNumber(), null); 
      } 
      else 
      { 
       /* Adding */ 
       getDatabase().insert(TABLE_NAME_BUSSES, null, values); 
      } 
     } 
     catch(Exception e) 
     { 
      Log.e(LOG_TAG, "Error occurred while adding to/updating database!", e); 

      result = false; 
     } 

     /* If the database is opened, close it */ 
     if(isOpened) 
     { 
      closeDB(); 
     } 

     /* Return the result */ 
     return result; 
    } 

    @Override 
    public int get(Bus entry) 
    { 
     /* Resulting number */ 
     int number = -1; 

     /* If the database is not opened, open it */ 
     if(!isOpened) 
     { 
      openDB(); 
     } 

     /* Column to select which is just number */ 
     String[] columns = new String[] 
     { 
      KEY_BUSSES_NUMBER 
     }; 

     /* Cursor to query the database */ 
     Cursor cursor = getDatabase().query(TABLE_NAME_BUSSES, columns, 
              KEY_BUSSES_SOURCE + "=" + (entry.getSource() != null ? "\"" + entry.getSource() + "\"" : "NULL") + " AND " + 
              KEY_BUSSES_DESTINATION + "=" + (entry.getDestination() != null ? "\"" + entry.getDestination() + "\"" : "NULL") + " AND " + 
              KEY_BUSSES_ROUTE + "=" + (entry.getRoute() != null ? "\"" + entry.getRoute() + "\"" : "NULL") + " AND " + 
              KEY_BUSSES_ISSTARRED + "=" + (entry.isStarred() ? 1 : 0) + " AND " + 
              KEY_BUSSES_TIMESH + "=" + (new Gson().toJson(entry.getTimesH()) != null ? "\"" + new Gson().toJson(entry.getTimesH()) + "\"" : "NULL") + " AND " + 
              KEY_BUSSES_TIMESC + "=" + (new Gson().toJson(entry.getTimesC()) != null ? "\"" + new Gson().toJson(entry.getTimesC()) + "\"" : "NULL") + " AND " + 
              KEY_BUSSES_TIMESP + "=" + (new Gson().toJson(entry.getTimesP()) != null ? "\"" + new Gson().toJson(entry.getTimesP()) + "\"" : "NULL"), 
              null, null, null, null); 

     /* If successfully queried */ 
     if(cursor != null) 
     { 
      /* If any match is found */ 
      if(cursor.getCount() > 0) 
      { 
       /* Go to the first match */ 
       cursor.moveToFirst(); 

       /* Set the resulting number */ 
       number = cursor.getInt(cursor.getColumnIndex(KEY_BUSSES_NUMBER)); 
      } 
     } 

     /* If the database is opened, close it */ 
     if(isOpened) 
     { 
      closeDB(); 
     } 

     /* Return the result */ 
     return number; 
    } 
} 
+0

您確定在嘗試插入數據庫之前調用'openDB()'嗎? – Luksprog 2013-04-20 18:21:43

+0

是的。在addOrUpdate()方法的開始處有一個檢查。 if(!isOpened) { openDB(); } – 2013-04-20 18:25:16

回答

0

int temp = get(entry);關閉數據庫。您不應該在數據庫類中打開和關閉數據庫。在實例化數據庫類的類中打開並關閉。

+0

這解決了我的問題。謝謝。 :) – 2013-04-20 19:25:25

相關問題