-2

Possible Duplicate:
getting nullpointer exception when clicking on checkdata button
nullpointer exception raises when i click on the button

i was unable to handle null pointer exception in my code

getting an error in this line as DataManipulator.createCertificatesEntry( (Certificates) testCertificates); this is my createCertificatesEntry

 public void createCertificatesEntry(Certificates certificates) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            certificates.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
            ContentValues cv = new ContentValues();
            cv.put(KEY_IMG, out.toByteArray());
            mDb.insert(CERTIFICATES_TABLE,  null, cv);
        }

is anything wrong with this code

this is logcat
05-29 12:57:42.577: E/AndroidRuntime(625): FATAL EXCEPTION: main
05-29 12:57:42.577: E/AndroidRuntime(625): java.lang.RuntimeException: Unable to start activity ComponentInfo{list.certificates/list.certificates.CheckData}: java.lang.NullPointerException
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.os.Looper.loop(Looper.java:137)
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.app.ActivityThread.main(ActivityThread.java:4340)
05-29 12:57:42.577: E/AndroidRuntime(625):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 12:57:42.577: E/AndroidRuntime(625):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 12:57:42.577: E/AndroidRuntime(625):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-29 12:57:42.577: E/AndroidRuntime(625):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-29 12:57:42.577: E/AndroidRuntime(625):  at dalvik.system.NativeStart.main(Native Method)
05-29 12:57:42.577: E/AndroidRuntime(625): Caused by: java.lang.NullPointerException
05-29 12:57:42.577: E/AndroidRuntime(625):  at list.certificates.DataManipulator.createCertificatesEntry(DataManipulator.java:61)
05-29 12:57:42.577: E/AndroidRuntime(625):  at list.certificates.CheckData.onCreate(CheckData.java:28)
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.app.Activity.performCreate(Activity.java:4465)
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-29 12:57:42.577: E/AndroidRuntime(625):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
05-29 12:57:42.577: E/AndroidRuntime(625):  ... 11 more
Community
  • 1
  • 1
samsun
  • 37
  • 1
  • 9
  • I think I answered the same in http://stackoverflow.com/questions/10794559/getting-nullpointer-exception-when-clicking-on-checkdata-button/10794752#10794752 Please let me know any thing you can't understand from there ...... – Dheeresh Singh May 29 '12 at 09:01
  • Downvoted because you have asked the same question before – Simon May 29 '12 at 09:15

2 Answers2

0

as you passing the "Object testCertificates" which is null in Activity at line DataManipulator.createCertificatesEntry( (Certificates) testCertificates);

in DataManipulator.createCertificatesEntry method certificates is null so certificates.getBitmap() will thorugh null pointer excption here

 public void createCertificatesEntry(Certificates certificates) 
    { 
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     certificates.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
     ContentValues cv = new ContentValues();
     cv.put(KEY_IMG, out.toByteArray());
     mDb.insert(CERTIFICATES_TABLE, null, cv);
     } 

pass not null value in DataManipulator.createCertificatesEntry from Activity by using this line

Certificates testCertificates = new Certificates(BitmapFactory.decodeFile(Context.STORAGE_SERVICE));

above

DataManipulator.createCertificatesEntry( (Certificates) testCertificates);

Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

one of the object in your this chain is null list.certificates.DataManipulator

Yahya Arshad
  • 1,626
  • 2
  • 19
  • 34