2013-04-20 32 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

相关问题