尝试保存到SQLite数据库时,应用程序崩溃

时间:2017-06-17 09:36:37

标签: android sqlite

当我尝试保存没有图像的联系人时,我的应用程序崩溃,但保存图像时,它可以正常工作。

这是我在没有图片时保存的结果:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getAbsolutePath()' on a null object reference

崩溃的地方是:

if (dbHelper.insertPerson(
image.getAbsolutePath().toString(), <-- Here it fails

有人可以帮忙吗?

由于

修改

以下是代码:

Bitmap imagecam;
private File image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    personID = getIntent().getIntExtra(FragmentJob.KEY_EXTRA_CONTACT_ID, 0);
    setContentView(R.layout.activity_edit);
    verifyStoragePermissions(this);

    DIRECTORY = Environment.getExternalStorageDirectory().getPath() + "/DigitSign/";
    pic_name = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    StoredPath = DIRECTORY + pic_name + ".png";

    btn_get_sign = (Button) findViewById(R.id.SignatureButton);

    // Method to create Directory, if the Directory doesn't exists
    file = new File(DIRECTORY);
    if (!file.exists()) {
        file.mkdir();
    }

 ....

 dbHelper = new ExampleDBHelper(this);

    if (personID > 0) {
        saveButton.setVisibility(View.GONE);
        buttonLayout.setVisibility(View.VISIBLE);

        rs = dbHelper.getPerson(personID);
        rs.moveToFirst();
        custsign = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_PROFIMAGE));

 if (!rs.isClosed()) {
            rs.close();
 imagecam = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(custsign), 512, 384);
            profImage.setImageBitmap(imagecam);
            profImage.setFocusable(false);
            profImage.setClickable(false);

 ....

 @Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.saveButton:
            persistPerson();
            return;
        case R.id.editButton:
            saveButton.setVisibility(View.VISIBLE);
            buttonLayout.setVisibility(View.GONE);

            profImage.setEnabled(false);
            profImage.setFocusableInTouchMode(false);
            profImage.setClickable(false);

 ....

 public void persistPerson() {
    if (personID > 0) {

        if (dbHelper.updatePerson(personID,
           image.getAbsolutePath().toString(),

 ....

  if (dbHelper.insertPerson(
     image.getAbsolutePath().toString(),

然后在DB Class中:

 public static final String PERSON_PROFIMAGE = "profileimage";

 ....

 PERSON_PROFIMAGE + " TEXT, " +  <-- Saving image path

 ....

 @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + PERSON_TABLE_NAME);
    onCreate(db);
}

public boolean insertPerson(String profileimage,
SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(PERSON_PROFIMAGE, profileimage);

 ....

 public int numberOfRows() {
    SQLiteDatabase db = this.getReadableDatabase();
    int numRows = (int) DatabaseUtils.queryNumEntries(db, PERSON_TABLE_NAME);
    return numRows;
}

public boolean updatePerson(Integer id,
                            String profileimage,
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
contentValues.put(PERSON_PROFIMAGE, profileimage);

2 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情

if (dbHelper.insertPerson(
                    image.getAbsolutePath()==null ? "" : image.getAbsolutePath().toString(),

注意:在获取absolutePath之前检查NPE。

希望这适合你!

答案 1 :(得分:0)

  

崩溃的原因

在你的代码图片为null时,当你调用image.getAbsolutePath()时,它会创建一个空指针异常,app会崩溃,更好的解决方案是null检查

我正在修改您的代码

String imagePath = "";
if(image!=null && image.getAbsolutePath()!=null){
   imagePath = image.getAbsolutePath().toString(); 
}

if (dbHelper.insertPerson(
imagePath , <-- Here it fails