I am currently trying to add a bitmap image to a ImageView but it is throwing a NPE. I have no idea what the problem is as it has worked in the past. I have just recreated the project because of some issues with the previous workspace.

Here is the method where I add the Bitmap to the ImageView:

private void insertElement(String data, String key, int i, Bitmap bm,
        boolean flag, boolean b) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View newRow = inflater.inflate(R.layout.row, null, false);
    newRow.setLayoutParams(new TableRow.LayoutParams(
            TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT));

    TextView dataTextView = (TextView) newRow
            .findViewById(R.id.rowTextView);
    dataTextView.setText("\t " + data);
    ImageView iv = (ImageView) newRow.findViewById(R.id.imageView);
    newRow.setId(i);
    newRow.setOnClickListener(new OnClickListener() {
        //ommited
    });

    View v = new View(this);
    v.setLayoutParams(new TableRow.LayoutParams(
            TableRow.LayoutParams.MATCH_PARENT, 1));
    v.setBackgroundColor(Color.argb(18, 111, 111, 111));

    dataTextView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    int sf = 1;
    if (i == 0) {
        sf = 2;
        dataTextView.setShadowLayer(3, 0, 0, Color.BLACK);
        dataTextView.setTextColor(getResources().getColor(R.color.white));
    }

    if (bm == null && iv == null) {
        iv.setVisibility(View.GONE);
    } else {
        iv.setImageBitmap(bm);  // <---------- Error here

        /**             !original code!
         * iv.setImageBitmap(getCroppedBitmap(Bitmap.createScaledBitmap(
                Bitmap.createScaledBitmap(bm, bm.getWidth() / 2 * sf,
                        bm.getWidth() / 2 * sf, true),
                dataTextView.getMeasuredHeight() * sf,
                dataTextView.getMeasuredHeight() * sf, true), iv, key));
         */

    }

    if (flag) {
        dataTextView.setTextSize(40);

        tableScrollView.addView(newRow, i);
    } else {
        tlcard.addView(newRow, i - 1);
        if (!b) {
            tlcard.addView(v, i);
        }
    }
}

The line that throws the exception is iv.setImageBitmap(bm). Obviously, bm is not null, and iv is not either.

Here is the xml file I inflate iv from:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin" >
<RelativeLayout
    android:id="@+id/rowRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/rowImageView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:scaleType="center" />

    <LinearLayout
        android:layout_width="1dp"
        android:layout_height="match_parent" >

        <View style="@style/Divider" />
    </LinearLayout>

    <TextView
        android:id="@+id/rowTextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView"
        android:textColor="@color/black"
        android:textSize="20sp"> </TextView>
</RelativeLayout>
</TableRow>

I have tried to identify the issue by checking if any of the components are null, but iv and bm are not.

Edit: here is the full error log:

08-31 21:29:39.693: W/System.err(32495): java.lang.NullPointerException
08-31 21:29:39.693: W/System.err(32495):    at com.tabcards.android.Search.insertElement(Search.java:422)
08-31 21:29:39.693: W/System.err(32495):    at com.tabcards.android.Search.createUI(Search.java:245)
08-31 21:29:39.693: W/System.err(32495):    at com.tabcards.android.Search.access$1(Search.java:224)
08-31 21:29:39.693: W/System.err(32495):    at com.tabcards.android.Search$1$1.run(Search.java:116)
08-31 21:29:39.693: W/System.err(32495):    at android.os.Handler.handleCallback(Handler.java:725)
08-31 21:29:39.693: W/System.err(32495):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-31 21:29:39.693: W/System.err(32495):    at android.os.Looper.loop(Looper.java:158)
08-31 21:29:39.693: W/System.err(32495):    at android.app.ActivityThread.main(ActivityThread.java:5751)
08-31 21:29:39.693: W/System.err(32495):    at java.lang.reflect.Method.invokeNative(Native Method)
08-31 21:29:39.693: W/System.err(32495):    at java.lang.reflect.Method.invoke(Method.java:511)
08-31 21:29:39.693: W/System.err(32495):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
08-31 21:29:39.693: W/System.err(32495):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
08-31 21:29:39.698: W/System.err(32495):    at dalvik.system.NativeStart.main(Native Method)

Related posts

Recent Viewed