0

I am trying to submit details from a form to the local database on the device. I am getting a null pointer which seems to be occuring in this section of code. I have a feeling it might be to do with the way I am trying to get the image from the ImageView.

Any ideas?

    addContactBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "Add Contact", Toast.LENGTH_SHORT).show();
            i = new Intent(v.getContext(), ViewActivity.class);
            //Add contact to database
            dbAdapter.addContact(nameBox.getText().toString(), numberBox.getText().toString(), emailBox.getText().toString(), imgHolder.getDrawingCache());
            startActivity(i);
        }
    });
}

This is the addContact() method:

public void addContact(String name, String number, String email, Bitmap img){
    ContentValues values = new ContentValues();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    img.compress(Bitmap.CompressFormat.PNG, 100, out);

    values.put(COLUMN_NAME, name);
    values.put(COLUMN_NUMBER, number);
    values.put(COLUMN_EMAIL, email);
    values.put(COLUMN_IMG, out.toByteArray());

    db.insert(TABLE_NAME, null, values);
    Toast.makeText(context, "Contact Added Successfully!", Toast.LENGTH_SHORT).show();
}

This is the log output:

01-17 23:57:16.939: E/AndroidRuntime(4582): FATAL EXCEPTION: main
01-17 23:57:16.939: E/AndroidRuntime(4582): java.lang.NullPointerException
01-17 23:57:16.939: E/AndroidRuntime(4582):     at com.example.contactappca2.AddActivity$2.onClick(AddActivity.java:73)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at android.view.View.performClick(View.java:3549)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at android.view.View$PerformClick.run(View.java:14400)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at android.os.Handler.handleCallback(Handler.java:605)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at android.os.Looper.loop(Looper.java:154)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at android.app.ActivityThread.main(ActivityThread.java:4944)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at java.lang.reflect.Method.invokeNative(Native Method)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at java.lang.reflect.Method.invoke(Method.java:511)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-17 23:57:16.939: E/AndroidRuntime(4582):     at dalvik.system.NativeStart.main(Native Method)
Javacadabra
  • 5,578
  • 15
  • 84
  • 152

2 Answers2

2

Its better if you directly pass the Bitmap rather than imgHolder.getDrawableCache(), it should work,

BitmapDrawable drawable = (BitmapDrawable) imgHolder.getDrawable();
Bitmap bitmap = drawable.getBitmap();

i = new Intent(v.getContext(), ViewActivity.class);
//Add contact to database
dbAdapter.addContact(nameBox.getText().toString(), numberBox.getText().toString(), emailBox.getText().toString(), bitmap);
startActivity(i);
abhi
  • 503
  • 6
  • 24
  • 1
    look at the answers [here](http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null). – abhi Jan 18 '13 at 00:40
0

try to call layout(int, int, int, int) on the corresponding layout before you call imgHolder.getDrawingCache();

seen here: Android 2.1 View's getDrawingCache() method always returns null

Community
  • 1
  • 1
sschrass
  • 7,014
  • 6
  • 43
  • 62