title: Error:android.view.View$OnClickListener on a null object reference
body: <p>I'm getting a NullPointerException when I try to set a <code>clickListener</code> to ImageView.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>activity_list_funny.xml</p>
  
  <pre><code>&lt;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"&gt;
  &lt;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"/&gt;
  &lt;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"
      /&gt;
  &lt;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" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>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</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>ArrayList&lt;HashMap&lt;String, String&gt;&gt; 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&lt;&gt;(this, R.layout.activity_list_funny);
      i.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
          }
      });
  </code></pre>
  
  <p>}</p>