尝试在空对象引用上调用虚拟方法'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)'

时间:2019-07-08 12:07:52

标签: java android android-sqlite

我正在学习SQlite,我有两节课:DatabaseHelperMainActivity。第一个负责数据库。我想创建一个新表,但是在MainActivity类中。

但是我收到此错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference

代码是:

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "ABC.db";

    @SuppressLint("SdCardPath")
    public static final String DBLOCATION = "/data/data/com.example.abc/databases/";
    private Context mContext;
    private SQLiteDatabase mDatabase;


    public DatabaseHelper(Context context) {

        super(context, DBNAME, null, 1);
        this.mContext = context;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    public void openDatabase() {

        String dbPath = mContext.getDatabasePath(DBNAME).getPath();
        if (mDatabase != null && mDatabase.isOpen()) {
            return;
        }
        mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    public void closeDatabase() {

        if (mDatabase != null) {
            mDatabase.close();
        }

    }

    public List<ABCMODEL> getListABC() {

        ABCModel abcModel = null;
        List<ABCModel> abcModelList = new ArrayList<>();

      openDatabase();
            Cursor cursor = mDatabase.rawQuery("SELECT * FROM abctable ", null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            abcModel = new ABCModel(cursor.getString(0),cursor.getString(0));
            abcModelList.add(abcModel);
            cursor.moveToNext();
        }
        cursor.close();

        closeDatabase();

        return abcModelList;

    }

    }

MainActivity.java

     DatabaseHelper mdatabaseHelper;
     SQLiteDatabase msqliteDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mdatabaseHelper= new DatabaseHelper(this);

        File database= getApplicationContext().getDatabasePath(DatabaseHelper.DBNAME);
        if(false== database.exists()){
            mdatabaseHelper.getReadableDatabase();

            if(copyDatabase(this)){
                Toast.makeText(this,"Copy database succes",Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(this,"Copy database error",Toast.LENGTH_SHORT).show();
                return;
            }
        }

        mdatabaseHelper.openDatabase();
         String query;

        query=
                "CREATE TABLE abctable2(\n" +
                        "Text  TEXT,\n" +
                        "Text2 TEXT\n" +
                        ");";

        msqliteDatabase.execSQL(query);
        mdatabaseHelper.closeDatabase();    
    }

1 个答案:

答案 0 :(得分:1)

您没有实例化 msqliteDatabase ,因此当您尝试msqliteDatabase.execSQL(query);

时它将为空。
  • 使用SQLiteDatabase msqliteDatabase;仅为 SQLiteDatabase 对象保留内存,它不会实例化它,因此指向该对象的指针尚未设置/初始化,因此为空。 / li>

您可以添加

msqliteDatabase = mdatabaseHelper.getWritableDatabase();

在使用 msqliteDatabase 之前。