title: Null Pointer Exception button setOnClickListener(new OnClickListener ()
body: <p>Showing button in a List row and wanna allow user use that button to add List Item to Cart, but whenever i am loading list getting error Unfortunately App has stopped, if i do not use button in my code not getting any error.</p>
  
  <p><em>So here my question what is wrong in my code while i am trying to work with button?</em></p>
  
  <p>This is the line, where i am getting error:</p>
  
  <pre><code>    mImgAddCart.setOnClickListener(new OnClickListener() {
  </code></pre>
  
  <p><em>Logcat:</em></p>
  
  <pre><code>07-01 05:57:03.733: E/AndroidRuntime(779): FATAL EXCEPTION: main
  07-01 05:57:03.733: E/AndroidRuntime(779): java.lang.NullPointerException
  07-01 05:57:03.733: E/AndroidRuntime(779):  at com.example.sample.ItemsActivity$MyAsyncTask.onPostExecute(ItemsActivity.java:191)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at com.example.sample.ItemsActivity$MyAsyncTask.onPostExecute(ItemsActivity.java:1)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at android.os.AsyncTask.finish(AsyncTask.java:631)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at android.os.Handler.dispatchMessage(Handler.java:99)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at android.os.Looper.loop(Looper.java:137)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at android.app.ActivityThread.main(ActivityThread.java:5041)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at java.lang.reflect.Method.invokeNative(Native Method)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at java.lang.reflect.Method.invoke(Method.java:511)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  07-01 05:57:03.733: E/AndroidRuntime(779):  at dalvik.system.NativeStart.main(Native Method)
  07-01 05:57:07.172: E/Trace(806): error opening trace file: No such file or directory (2)
  </code></pre>
  
  <p><em>LazyAdapter.java:</em></p>
  
  <pre><code>     public class LazyAdapter extends BaseAdapter {
  
      private Activity activity;
      private ArrayList&lt;HashMap&lt;String, String&gt;&gt; data;
      private static LayoutInflater inflater=null;
      public ImageLoader imageLoader; 
      String itemTitle, itemCost;
  
      public LazyAdapter(Activity a, ArrayList&lt;HashMap&lt;String, String&gt;&gt; d) {
          activity = a;
          data=d;
          inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          imageLoader=new ImageLoader(activity.getApplicationContext());
      }
  
      public int getCount() {
          return data.size();
      }
  
      public Object getItem(int position) {
          return position;
      }
  
      public long getItemId(int position) {
          return position;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          View vi=convertView;
          if(convertView==null)
              vi = inflater.inflate(R.layout.listrow_items, null);
  
          final TextView title = (TextView)vi.findViewById(R.id.title); 
          final TextView cost = (TextView)vi.findViewById(R.id.cost); 
          ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image);
  
  
          HashMap&lt;String, String&gt; item = new HashMap&lt;String, String&gt;();
          item = data.get(position);
  
          // Setting all values in listview
          title.setText(item.get(ItemsActivity.KEY_TITLE));
          cost.setText(item.get(ItemsActivity.KEY_COST));
          imageLoader.DisplayImage(item.get(com.example.sample.ItemsActivity.KEY_THUMB_URL), thumb_image);
  
          //Button mImgAddCart
          Button mImgAddCart = (Button) vi.findViewById(R.id.btn_add_to_order);
              mImgAddCart.setOnClickListener(new OnClickListener() {
  
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                      itemTitle = title.getText().toString();
                      itemCost = cost.getText().toString();                  
  
                      if (Constants.sItem_Detail.size() &lt;= 0) {
                          HashMap&lt;String, String&gt; sTempObj = new HashMap&lt;String, String&gt;();
                          sTempObj.put(KEY_TITLE, itemTitle);
                          sTempObj.put(KEY_COST, itemCost);
                          Constants.sItem_Detail.add(sTempObj);               
                          }                   
  
                      AlertDialog.Builder alertdialog = new AlertDialog.Builder(
                              ItemsActivity.this);
                      alertdialog.setTitle(getResources()
                              .getString(R.string.app_name));
                      alertdialog.setMessage("Item Added to Cart");
  
                      alertdialog.setPositiveButton("OK",
                              new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog,
                                          int which) 
                                  {
                                      activity.finish();
                                  }
                              });     
                      alertdialog.show();             
                  }
              });
  
          return vi;
      }
  }
  </code></pre>
  
  <p><strong>Line:</strong> </p>
  
  <pre><code>sTempObj.put(KEY_TITLE, itemTitle);
  </code></pre>
  
  <p><strong>Getting:</strong></p>
  
  <pre><code>KEY_TITLE cannot be resolved to a variable
  </code></pre>
  
  <p><strong>Line:</strong></p>
  
  <pre><code>AlertDialog.Builder alertdialog = new AlertDialog.Builder(ItemsActivity.this);
  </code></pre>
  
  <p><strong>Getting:</strong></p>
  
  <pre><code>No enclosing instance of the type ItemsActivity is accessible in scope
  </code></pre>
  
  <p><strong>Line:</strong></p>
  
  <pre><code>alertdialog.setTitle(getResources().getString(R.string.app_name));
  </code></pre>
  
  <p><strong>Getting:</strong></p>
  
  <pre><code>The method getResources() is undefined for the type new View.OnClickListener(){}
  </code></pre>