2

重複の可能性:
ボタンをクリックするとnullpointer例外が発生します

Checkdataボタンをクリックすると、CheckData.java DataManipulator.createCertificatesEntry((Certificates)testCertificates);でnullpointer例外が発生します。

public class DataManipulator {
    public static final String KEY_IMG = "image";

     private DatabaseHelper mDbHelper;
        private SQLiteDatabase mDb;

        private static final String DATABASE_NAME = "DBtest";
        private static final int DATABASE_VERSION = 1;

    private static final String CERTIFICATES_TABLE = "certificates";

    private static final String CREATE_CERTIFICATES_TABLE = "create table "+CERTIFICATES_TABLE+" (" +KEY_IMG+" blob not null) ";

    private final Context mCtx;
    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_CERTIFICATES_TABLE);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS "+CERTIFICATES_TABLE);
            onCreate(db);
        }
    }
    public void Reset() { mDbHelper.onUpgrade(this.mDb, 1, 1); }

    public DataManipulator(Context ctx) {
        mCtx = ctx;
        mDbHelper = new DatabaseHelper(mCtx);
    }

    public DataManipulator open() throws SQLException {
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() { mDbHelper.close(); }

    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);
    }
    public Certificates getFirstCertificatesFromDB() throws SQLException {
        Cursor cur = mDb.query(true, CERTIFICATES_TABLE,  new String[] {KEY_IMG}, null, null, null, null, null, null);
        if(cur.moveToFirst()) {
            byte[] blob = cur.getBlob(cur.getColumnIndex(KEY_IMG));
            ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            cur.close();
            return new Certificates(bitmap);
        }
        cur.close();
        return null;
    }    
}

これは、DataManipulator.javacertificates.getBitmap ()。compress(Bitmap.CompressFormat.PNG、100、out);の60行目です。

public class CheckData extends ListActivity  {     
    TextView selection;
    DataManipulator dm;
    private DataManipulator DataManipulator;
    protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.check);
        dm = new DataManipulator(this);

        LinearLayout layout = new LinearLayout(this);
        ImageView image = new ImageView(this);
        DataManipulator = new DataManipulator(this);

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

        DataManipulator.open();
        DataManipulator.createCertificatesEntry( (Certificates) testCertificates);
        DataManipulator.close(); 

        DataManipulator.close();

        testCertificates = null;

        DataManipulator.open();
        testCertificates = DataManipulator.getFirstCertificatesFromDB();
        DataManipulator.close();

        image.setImageBitmap(((Certificates) testCertificates).getBitmap());
        setContentView(layout);
    }
}   

これは、CheckData.java DataManipulator.createCertificatesEntry((Certificates)testCertificates);の29行目です。

これは私のlogcatです

05-29 14:51:46.908: E/AndroidRuntime(741): FATAL EXCEPTION: main
05-29 14:51:46.908: E/AndroidRuntime(741): java.lang.RuntimeException: Unable to start activity ComponentInfo{list.certificates/list.certificates.CheckData}: java.lang.NullPointerException
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.os.Looper.loop(Looper.java:137)
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.app.ActivityThread.main(ActivityThread.java:4340)
05-29 14:51:46.908: E/AndroidRuntime(741):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 14:51:46.908: E/AndroidRuntime(741):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 14:51:46.908: E/AndroidRuntime(741):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-29 14:51:46.908: E/AndroidRuntime(741):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-29 14:51:46.908: E/AndroidRuntime(741):  at dalvik.system.NativeStart.main(Native Method)
05-29 14:51:46.908: E/AndroidRuntime(741): Caused by: java.lang.NullPointerException
05-29 14:51:46.908: E/AndroidRuntime(741):  at list.certificates.DataManipulator.createCertificatesEntry(DataManipulator.java:60)
05-29 14:51:46.908: E/AndroidRuntime(741):  at list.certificates.CheckData.onCreate(CheckData.java:29)
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.app.Activity.performCreate(Activity.java:4465)
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-29 14:51:46.908: E/AndroidRuntime(741):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
05-29 14:51:46.908: E/AndroidRuntime(741):  ... 11 more
4

1 に答える 1

0

アクティビティの行 DataManipulator.createCertificatesEntry((Certificates) testCertificates); で null である「Object testCertificates」を渡すと、

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

したがって、 createCertificatesEntry メソッドにチェックを入れるか

また

この行を使用して、Activity から DataManipulator.createCertificatesEntry に null 以外の値を渡します

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

その上

DataManipulator.createCertificatesEntry( (証明書) testCertificates);

于 2012-05-29T07:23:51.610 に答える