0

我有使用数据库的源代码。
我的数据库代码在一个类中,当我从这个类中插入任何东西时没有问题。但是,如果在其他类中调用该方法,则会从该行显示错误消息“java.lang.nullpointerexception”SQLiteDatabase sd = getWritableDatabase();

这是我的主要课程:

public static class SchemaHelper extends SQLiteOpenHelper {
         private static final String DATABASE_NAME = "adv_data.db";
         // TOGGLE THIS NUMBER FOR UPDATING TABLES AND DATABASE
         private static final int DATABASE_VERSION = 1;
         SchemaHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
         }

         @Override
         public void onCreate(SQLiteDatabase db) {

            // CREATE JOINT TABLE
             db.execSQL("CREATE TABLE"+ JointTable.TABLE_NAME  
            + "("+ JointTable.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"         
            + JointTable.NAME +" TEXT, "
            + JointTable.FAMILY + " TEXT,"
            + JointTable.ADDRESS+"TEXT,"
            + JointTable.PARTICIPATIONNUM +"Text,"
            + JointTable.RECOGNITIONNUM + "TEXT,"
            + JointTable.USAGE +"TEXT );");


         }
         @Override
         public void onUpgrade(SQLiteDatabase db, int oldVersion,
             int newVersion) {
             Log.w("LOG_TAG", "Upgrading database from version "
             + oldVersion + " to " + newVersion + ", which will destroy all old data");
             // KILL PREVIOUS TABLES IF UPGRADED
             db.execSQL("DROP TABLE IF EXISTS "+ JointTable.TABLE_NAME);

             // CREATE NEW INSTANCE OF SCHEMA
             onCreate(db);
                        } // End of onUpdate class

=============================== 插入表================ ====================

         public  long addJoit(String name, String family, String address,
                 String participationNum ,String recognitionNum, String usage ){

             ContentValues cv= new ContentValues();
             cv.put(JointTable.NAME, name);
             cv.put(JointTable.FAMILY, family);
             cv.put(JointTable.ADDRESS, address);
             cv.put(JointTable.PARTICIPATIONNUM, participationNum);
             cv.put(JointTable.RECOGNITIONNUM,recognitionNum);
             cv.put(JointTable.USAGE, usage);

             SQLiteDatabase sd = this.getWritableDatabase();// this line get the error
             long result= sd.insert(JointTable.TABLE_NAME,
                     JointTable.NAME, cv);
             return result;
         }





             }
}

这是调用该方法的另一个类

MainActivity.SchemaHelper sh = new MainActivity.SchemaHelper(null);

long result=  sh.addJoit("name", "family", "address", "participationNum", "recognitionNum", "usage");

我想知道原因谢谢

4

3 回答 3

1

通过以这种方式调用该方法,我的问题已解决:

MainActivity.SchemaHelper sh = new MainActivity.SchemaHelper(getBaseContext());

构造函数需要getBaseContext()方法。

感谢大家帮助我。

于 2014-11-16T05:38:56.063 回答
0

It seems that sh is null because you pass null in SchemaHelper constructor,So change

MainActivity.SchemaHelper sh = new MainActivity.SchemaHelper(null);

to

SchemaHelper sh = new SchemaHelper(MainActivity.this);

And also add space in your create table statment,.i.e. Change

  db.execSQL("CREATE TABLE"+ JointTable.TABLE_NAME  
            + "("+ JointTable.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"         
            + JointTable.NAME +" TEXT, "
            + JointTable.FAMILY + " TEXT,"
            + JointTable.ADDRESS+"TEXT,"
            + JointTable.PARTICIPATIONNUM +"Text,"
            + JointTable.RECOGNITIONNUM + "TEXT,"
            + JointTable.USAGE +"TEXT );");

to

  db.execSQL("CREATE TABLE "+ JointTable.TABLE_NAME  
            + "("+ JointTable.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"         
            + JointTable.NAME + " TEXT, "
            + JointTable.FAMILY + " TEXT,"
            + JointTable.ADDRESS + " TEXT,"
            + JointTable.PARTICIPATIONNUM +" TEXT,"
            + JointTable.RECOGNITIONNUM + " TEXT,"
            + JointTable.USAGE + " TEXT);");

And also set context properly ,i.e. change

 SchemaHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
         }

to

 Context mContext;
 SchemaHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
         mContext = context;
         }

and change

 SQLiteDatabase sd = this.getWritableDatabase();

to

 SQLiteDatabase sd = mContext.getWritableDatabase();
于 2014-11-15T11:21:41.013 回答
0

为简单起见,制作不同的类。

 public class Insert {

    private Context context;
    private SchemaHelper schemaHelper;
    private SQLiteDatabase database;


    public Insert(Context context) {
        this.context = context;
    }


    public  long addJoit(String name, String family, String address,
                         String participationNum ,String recognitionNum, String usage ){

        schemaHelper=new SchemaHelper(context);
        database=schemaHelper.getWritableDatabase();

        ContentValues cv= new ContentValues();
        cv.put(JointTable.NAME, name);
        cv.put(JointTable.FAMILY, family);
        cv.put(JointTable.ADDRESS, address);
        cv.put(JointTable.PARTICIPATIONNUM, participationNum);
        cv.put(JointTable.RECOGNITIONNUM,recognitionNum);
        cv.put(JointTable.USAGE, usage);


        long result= database.insert(JointTable.TABLE_NAME,
                null, cv);
        database.close();
        return result;
    }

}

在 MainActivity 类中,

Insert insert=new Insert(MainActivity.this);


  long add=insert.addJoit("name", "family", "address", "participationNum", "recognitionNum",        "usage");

此外,如果未创建表,请在 sql 语句中添加空格。IE

db.execSQL(" CREATE TABLE "+ JointTable.TABLE_NAME  
        + " ( "+ JointTable.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "         
        + JointTable.NAME + " TEXT, "
        + JointTable.FAMILY + " TEXT, "
        + JointTable.ADDRESS + " TEXT, "
        + JointTable.PARTICIPATIONNUM +" TEXT, "
        + JointTable.RECOGNITIONNUM + " TEXT, "
        + JointTable.USAGE + " TEXT ); ");
于 2014-11-15T12:15:39.333 回答