NullPointerException while trying to bind at SimpleCursorAdapter

Posted by hrskrs on Stack Overflow See other posts from Stack Overflow or by hrskrs
Published on 2014-08-22T16:58:47Z Indexed on 2014/08/22 22:21 UTC
Read the original article Hit count: 130

Filed under:

I have asked a question before where i found my mistake. However now i am facing with another problem. I have checked all the similar errors asked on StackOverflow but without success.Any help is appriciated.

The idea here is that i am getting image names from DB so depending on those names images from Drawable folder will be shown in a listView together with a description but im getting an error of NullPointException at setViewValue.

Here is the code snippet:

private void populateListView() {
    ListView customListView = (ListView)findViewById(R.id.lvCustom);

    Cursor cursor = DBhelper.getAllimages();

    startManagingCursor(cursor);

    String[] from = { DBhelper.COLUMN_PIC_URL, DBhelper.COLUMN_PIC_DESC};
    int[] to = {R.id.ivImg, R.id.tvTitle};
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.custom_listview_row, cursor, from, to, 0);

    cursorAdapter.setViewBinder(new ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            ImageView imageImageView = (ImageView)findViewById(R.id.ivImg);
            String[] imgNames = new String[cursor.getCount()];
            int[] imgResourceIds = new int[cursor.getCount()]; 
            for(int i=0; i<cursor.getCount(); i++){
                imgNames[i] = cursor.getString(cursor.getColumnIndex(DBhelper.COLUMN_PIC_URL));
                imgResourceIds[i] = getResources().getIdentifier(imgNames[i], "drawable", getPackageName());
                imageImageView.setImageResource(imgResourceIds[i]);
                cursor.moveToNext();
            }

            return true;
        }
    });

    customListView.setAdapter(cursorAdapter);

}

Here is the Error from LogCat:

enter image description here

I have tried to log the output of imgNames[i] where it returns the url pic from the DB correctly and imgResourceIds[i] where it return the image resource id correctly also(it does not return NULL but something like: 295731). But it stops at imageImageView.setImageResource(imgResourceIds[i]);

To see from where that NullPointerException is coming, i commented out imageImageView.setImageResource(imgResourceIds[i]);. This time imageNames(those with a TAG) and imgResourceIds(those system printed out) came correctly but doubled, when i removed cursor.MoveToNext() last row were doubled. Here is the screen shot of that: enter image description here

I have tried all the suggestions on stack about gettin a NullException but without success. Any idea where i am doing mistake?

© Stack Overflow or respective owner

Related posts about android