title: Android custom adapter getView() NullPointerException
body: <p>I am trying to create a grid view which has an Imageview.I tried to extend BaseAdapter but i struck with the problem ,I will be glad if someone can point out the mistake i am doing.</p>
  
  <p>Logcat:</p>
  
  <pre><code>07-10 19:29:00.881: D/AndroidRuntime(1678): Shutting down VM
  07-10 19:29:00.881: W/dalvikvm(1678): threadid=1: thread exiting with uncaught exception (group=0xb3a47b90)
  07-10 19:29:00.901: E/AndroidRuntime(1678): FATAL EXCEPTION: main
  07-10 19:29:00.901: E/AndroidRuntime(1678): Process: com.shrek.try2, PID: 1678
  07-10 19:29:00.901: E/AndroidRuntime(1678): java.lang.NullPointerException
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at com.shaastra.try2.MainActivity$lAdapter.getView(MainActivity.java:80)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.widget.GridView.onMeasure(GridView.java:1044)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.View.measure(View.java:16458)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.View.measure(View.java:16458)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.View.measure(View.java:16458)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.View.measure(View.java:16458)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.View.measure(View.java:16458)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.Choreographer.doFrame(Choreographer.java:532)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.os.Handler.handleCallback(Handler.java:733)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.os.Handler.dispatchMessage(Handler.java:95)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.os.Looper.loop(Looper.java:137)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at android.app.ActivityThread.main(ActivityThread.java:4998)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at java.lang.reflect.Method.invokeNative(Native Method)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at java.lang.reflect.Method.invoke(Method.java:515)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  07-10 19:29:00.901: E/AndroidRuntime(1678):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Its shows nullpointexception at <strong>line 80</strong> (mainactivity and customadapter are defined in mainAcitivity.java) :</p>
  
  <p>mainActivity.java:</p>
  
  <pre><code>package com.shrek.try2;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.GridView;
  import android.widget.ImageView;
  
  public class MainActivity extends Activity{// implements     OnItemClickListener {
  
      GridView myGrid;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      myGrid = (GridView) findViewById(R.id.gridView);
      myGrid.setAdapter(new lAdapter(this));
  }
  
  class lAdapter extends BaseAdapter
  {
      private final int[] lectimages={R.drawable.image_1,R.drawable.image_2,R.drawable.image_3,R.drawable.image_4,R.drawable.image_5,R.drawable.image_6,R.drawable.image_7,R.drawable.image_8,R.drawable.image_9};
      Context c;
      lAdapter(Context c)
      {
          this.c = c;
      }
      @Override
      public int getCount() {
          // TODO Auto-generated method stub
          return lectimages.length;
      }
  
      @Override
      public Object getItem(int arg0) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public long getItemId(int arg0) {
          // TODO Auto-generated method stub
          return 0;
      }
  
  
      @Override
      public View getView(int i, View convertView, ViewGroup parent)     {
          // TODO Auto-generated method stub
          View item;
              LayoutInflater inflater=(LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
              item = inflater.inflate(R.layout.single_item, parent, false);
              ImageView lect = (ImageView) findViewById(R.id.imageView_1);
              lect.setImageResource(lectimages[i]);
          return item;
  
      }
  
  }
  }
  </code></pre>
  
  <p>Which means line <strong>lect</strong> in getView method is null, hence findviewbybyid is returning null but I have taken correct id, images have been added to drawable and exact layout(single_item.xml)  has been inflated :</p>
  
  <p>single_item.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" &gt;
  
  &lt;ImageView
      android:id="@+id/imageView_1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/ic_launcher"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:contentDescription="imageViewDescription"/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and activity_main.xml:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" &gt;
  
  &lt;GridView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/gridView"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:numColumns="auto_fit"
      android:horizontalSpacing="0dp"
      android:verticalSpacing="0dp"
      android:columnWidth="120dp"
      android:stretchMode="spacingWidthUniform"
      /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>Please please</strong> help me, I am stuck in this problem for about <strong>10 hours</strong> already.</p>