-1

My app crashes when I try to save contacts with no images, but when saving with images it works.

this is what I get when saving with no images:

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

The line where it crashes is at:

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

Could some one please help?

Thanks

EDIT

Here is the code:

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(),

Then in the 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);
Keith
  • 17
  • 6

2 Answers2

0

you can try something like this

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

Note : Checking the NPE before getting absolutePath.

Hope this works for you!

android_griezmann
  • 3,557
  • 3
  • 14
  • 41
0

Reason for the crash

In your code image is null, when you call image.getAbsolutePath() it will create a null pointer exception and app will crash, better solution is null checking

I am modifiying your code

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

if (dbHelper.insertPerson(
imagePath , <-- Here it fails
EKN
  • 1,845
  • 13
  • 28