Shailender Narang Shailender Narang - 4 months ago 28
Android Question

Error:android.view.View$OnClickListener on a null object reference

I'm getting a NullPointerException when I try to set a

clickListener
to ImageView.

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


activity_list_funny.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="@+id/id1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="4dip"
android:textStyle="bold"
android:textIsSelectable="true"
android:textSize="15sp"
android:textColor="@android:color/holo_purple"/>
<TextView
android:id="@+id/name1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="1dip"
android:textStyle="bold"
android:textIsSelectable="true"
android:textSize="20sp"
/>
<ImageView
android:id="@+id/imgbtn_favorite1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:layout_alignBottom="@id/name1"
android:src="@drawable/ic_favorite_border_black_24dp"
android:contentDescription="@string/add_favr" />

</LinearLayout>


And Here When I Initiate ImageView By making An Object In My MainActivity And providing It An OnClickListener
And When I RuN It It force closes And Shows null object references

MainActivity.java

ArrayList<HashMap<String, String>> quotes;
ArrayAdapter adapter;
TextView t1,t2;
ListView list;
ImageView i;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_funny_quotes);
cd = new ConnectionDetector(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();
c = (CoordinatorLayout) findViewById(R.id.coordinatorLayout1);

**i=(ImageView)findViewById(R.id.imgbtn_favorite1);**

adapter = new ArrayAdapter<>(this, R.layout.activity_list_funny);
i.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});


}

Answer

Problem is:

You activity is using following layout: R.layout.activity_funny_quotes

But your button is inside: R.layout.activity_list_funny

This way, findViewById returns null and i == null

When will not be null

i=(ImageView)findViewById(R.id.imgbtn_favorite1); will return a valid ImageView only after you set the adapter to your ListView (list.setAdapter(adapter)). This way, the ImageView will be added to the screen and findViewById(R.id.imgbtn_favorite1) may return a valid ImageView

Even then, your list must not be empty. If the list is empty, no ImageView will be returned to the screen and you will keep receiveing null

There's another problem: if your list has several items, findViewById(R.id.imgbtn_favorite1) will return only the ImageView of the first item.

How to Fix

It's hard to tell you how to proper fix your issue.

You are trying to add a clickListener to an ImageView that will be displayed in your ListView.

Maybe, it is better to define a ListView.setOnItemClickListener() instead to add a clickListener to your ImageView