外部Sqlite数据库,无法插入数据库,Android Studio

时间:2018-09-05 04:44:50

标签: android sqlite oop android-sqlite

你们知道这段代码有什么问题吗?当我单击按钮以将数据插入sqlite外部数据库时。但似乎不起作用。 cv.put("name", txn.getName());处显示错误,也许我使用了错误的书写方式?

java页面

 public void onClick(View v) {
            try {
                SQLiteDatabase db = mSQLiteHelper.getWritableDatabase();
                ContentValues cv = new ContentValues();
                cv.put("name", txn.getName());
                cv.put("address", txn.getAddress());
                cv.put("phone", txn.getPhone());
                db.insert("Table1", null, cv);
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

这里是Error异常提示

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.waiku.work2.Model.getName()' on a null object reference

这是我的OOP模型,我对它的工作方式有些困惑。但是我遵循了youtube教程...

public class Model {
    private int id;
    private String name;
    private String address;
    private int phone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }
}

这是我的SQLite帮助器<​​/ p>

 public class SQLiteHelper extends SQLiteAssetHelper {
private static final String DB_NAME = "MyExternalDatabase.db";
private static final int DATABASE_VERSION = 1;

public SQLiteHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
}

2 个答案:

答案 0 :(得分:0)

  当应用程序尝试使用

NullPointerException   具有空值的对象引用。

尝试这种方式

try {
                SQLiteDatabase db = mSQLiteHelper.getWritableDatabase();
                ContentValues cv = new ContentValues();
                Model  modelOBJ = new Model();
                cv.put("name", modelOBJ.setName(your_name_textOBJ.getText().toString());
                cv.put("address", modelOBJ.setAddress(your_address_textOBJ.getText().toString());
                cv.put("phone",modelOBJ.setPhone(your_phone_textOBJ.getText().toString());
                db.insert("Table1", null, cv);
                db.close();
                } 
                catch (Exception e) 
               {
                e.printStackTrace();
               }

答案 1 :(得分:0)

您在空对象引用上得到了'Model.getName()',这可能是因为您没有初始化模型类。这样做:

 YourModelClassName txn= new YourModelClassName ();   // initialize your model class first
 txn.setName(youNameEditext.getText().toString()); // set entered name in model class
 public void onClick(View v) {
        try {
            SQLiteDatabase db = mSQLiteHelper.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put("name", txn.getName());  // get the entered name here.
            cv.put("address", txn.getAddress());
            cv.put("phone", txn.getPhone());
            db.insert("Table1", null, cv);
            db.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }