Android SQLite Database insert()方法导致NullPointerException

时间:2013-04-20 18:13:19

标签: android database sqlite nullpointerexception

当我尝试在我的数据库中插入一行时,我得到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;
    }
}

1 个答案:

答案 0 :(得分:0)

int temp = get(entry);中关闭数据库。您不应该在数据库类中打开和关闭数据库。在实例化数据库类的类中打开和关闭。