获取NullPointerExeption从sqlite读取图像到数组

时间:2015-05-19 11:21:10

标签: android arrays sqlite nullpointerexception

这是我从db获取图像并将它们放入可绘制数组的代码

表格结构:

_id INTEGER, question BLOB, answer1 BLOB, answer2 BLOB, answer3 BLOB, answer4 BLOB, answer5 BLOB, answer6 BLOB, asnwer7 BLOB, answer8 BLOB

这是我的代码:

public BitmapDrawable[][] getTableImage(String tbl_name) {

        qb.setTables(tbl_name);
        Cursor cursor = qb.query(db, null, null, null, null, null, null);
        int count = cursor.getCount();
        int count_column =cursor.getColumnCount()-1;
        BitmapDrawable[][] RavenQuery = new BitmapDrawable[count][count_column];

        cursor.moveToFirst();
        for (int i = 0; i < count; i++) {
            for (int j = 1; j < count_column; j++) {

// J从1开始,因为0是_id

                byte[] blob = cursor.getBlob(j);

                Log.i("blob.lentgh",String.valueOf(blob.length));
                ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
                Log.i("InputStream.lentgh",inputStream.toString());
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);


                if (bitmap==null){
                    RavenQuery[i][j-1]=RavenQuery[0][0];
                    continue;}




                RavenQuery[i][j-1] = new BitmapDrawable(bitmap);


            }
            cursor.moveToNext();
        }
        Log.w("Len", String.valueOf(RavenQuery.length));
        Log.w("b", "b = " +String.valueOf(RavenQuery[0][0].getBounds().height()) );
        cursor.close();
        Log.w("a", "a = " + String.valueOf(RavenQuery[0][0].getBounds().height()));
        return RavenQuery;
    }

当我调用此函数时,它返回一个长度为60的可绘制数组。

final Drawable[][] tbl_raven = sqlDb.getTableImage("tbl_t_reven");
        numberOfQuestions = tbl_raven.length;
        Log.w("height", "H = " + tbl_raven[0][0].getBounds());
        Log.w("lenreven", String.valueOf(numberOfQuestions));
        questionString = null;
        for (int i = 0; i < numberOfQuestions; i++) {
        CaseImages[i][0] = tbl_raven[i][0];
        CaseImages[i][1] = tbl_raven[i][1];
        CaseImages[i][2] = tbl_raven[i][2];
        CaseImages[i][3] = tbl_raven[i][3];
        CaseImages[i][4] = tbl_raven[i][4];
        CaseImages[i][5] = tbl_raven[i][5];
        CaseImages[i][6] = tbl_raven[i][6];
        CaseImages[i][7] = tbl_raven[i][7];
        CaseImages[i][8] = tbl_raven[i][8];
    }

但每个单元格都为空???

logcat的:

05-20 06:02:32.639    1452-1452/? I/SQLiteAssetHelper﹕ successfully opened database Tests.db
05-20 06:02:32.767    1452-1452/? I/blob.lentgh﹕ 5122
05-20 06:02:32.767    1452-1452/? I/InputStream.lentgh﹕ java.io.ByteArrayInputStream@3025d150
05-20 06:02:32.772    1452-1452/? I/blob.lentgh﹕ 1755
05-20 06:02:32.772    1452-1452/? I/InputStream.lentgh﹕ java.io.ByteArrayInputStream@39c0af49
05-20 06:02:32.775    1452-1452/? I/blob.lentgh﹕ 836
...(Showing Log.i for eachpicture)

05-20 06:02:33.158    1452-1452/? W/Len﹕ 60
05-20 06:02:33.158    1452-1452/? W/b﹕ b = 0
05-20 06:02:33.159    1452-1452/? W/a﹕ a = 0

 ... 
05-20 06:02:33.159    1452-1452/? W/height﹕ H = Rect(0, 0 - 0, 0)
05-20 06:02:33.159    1452-1452/? W/lenreven﹕ 60
(//CaseImages[i][0] = tbl_raven[i][0];)
05-20 06:02:33.159    1452-1452/? E/Error﹕ java.lang.NullPointerException: Attempt to read from null array

0 个答案:

没有答案