无法处理空指针异常

时间:2012-05-29 08:56:59

标签: android

  

可能重复:
  getting nullpointer exception when clicking on checkdata button
  nullpointer exception raises when i click on the button

我无法在我的代码中处理空指针异常

在此行中收到错误 DataManipulator.createCertificatesEntry((Certificates)testCertificates); 这是我的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);
        }

这段代码有什么问题

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

2 个答案:

答案 0 :(得分:0)

当你传递“Data testCertificates”时,它在Activity的DataManipulator.createCertificatesEntry((Certificates)testCertificates)中为null;

DataManipulator.createCertificatesEntry方法中的

方法证书为null,因此certificates.getBitmap()将在这里指出null指针

 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);
     } 

使用此行在Activity中的DataManipulator.createCertificatesEntry中传递非空值

证书testCertificates =新证书(BitmapFactory.decodeFile(Context.STORAGE_SERVICE));

以上

DataManipulator.createCertificatesEntry((Certificates)testCertificates);

答案 1 :(得分:0)

此链中的一个对象为null list.certificates.DataManipulator