title: How to Handle NullPointerException in android?
body: <pre><code>01-18 02:08:05.271: E/AndroidRuntime(24688): FATAL EXCEPTION: main
  01-18 02:08:05.271: E/AndroidRuntime(24688): java.lang.NullPointerException
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at com.example.tava.EchangingMessage$LoadData.onPostExecute(EchangingMessage.java:178)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at com.example.tava.EchangingMessage$LoadData.onPostExecute(EchangingMessage.java:1)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.AsyncTask.finish(AsyncTask.java:602)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.Handler.dispatchMessage(Handler.java:99)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.Looper.loop(Looper.java:154)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.app.ActivityThread.main(ActivityThread.java:4945)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at java.lang.reflect.Method.invokeNative(Native Method)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at java.lang.reflect.Method.invoke(Method.java:511)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  01-18 02:08:05.271: E/AndroidRuntime(24688):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>And this is my Code Activity Code  :</p>
  
  <pre><code>public class EchangingMessage extends Activity implements OnClickListener {
  
      private ProgressDialog pDialog;
      JSONParser jParser = new JSONParser();
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; DaftarRS;
      EditText textToSend;
  
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; arraylist;
  
      private static String url_daftar_rs = "http://192.168.137.1/www/Android_Login_Secure/detailMessage.php";
      private static String url_daftar_rs1 = "http://192.168.137.1/www/Android_Login_Secure/myFriends.php";
  
      public static final String TAG_SUCCESS = "success";
      public static final String TAG_DAFTAR_RS = "daftar_rs";
      public static final String TAG_ID_RS = "id_rs";
      public static final String TAG_NAMA_RS = "username";
      public static final String TAG_LINK_IMAGE_RS = "image";
      public static final String TAG_MESSAGE_RS = "messageText";
      public static final String TAG_TIME_RS = "sendAt";
      public static final String TAG_SEEN_RS = "readStatus";
  
      JSONArray daftar_rs = null;
  
      ListAdapterUserOnile adapterFirends;
  
      ImageView showOnline;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.exchange_message);
  arraylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
      }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
  
          switch (v.getId()) {
  
          case R.id.userOnline:
              userOnlineWindow();
              break;
  
          }
  
      }
  
      private void userOnlineWindow() {
          // TODO Auto-generated method stub
  
          LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                  .getSystemService(LAYOUT_INFLATER_SERVICE);
          View popupView = layoutInflater.inflate(R.layout.popup, null);
          final PopupWindow popupWindow = new PopupWindow(popupView,
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  
          Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
          btnDismiss.setOnClickListener(new Button.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  popupWindow.dismiss();
              }
          });
  
          popupWindow.showAsDropDown(showOnline, -190, -90);
  
          new LoadData().execute();
  
          popupWindow.setFocusable(true);
          popupWindow.update();
      }
  
      class LoadData extends AsyncTask&lt;String, String, String&gt; {
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              pDialog = new ProgressDialog(EchangingMessage.this);
              pDialog.setMessage("Loading Information...");
              pDialog.setIndeterminate(false);
              pDialog.setCancelable(false);
              pDialog.show();
          }
  
          @Override
          protected String doInBackground(String... arg0) {
  
              List&lt;NameValuePair&gt; params1 = new ArrayList&lt;NameValuePair&gt;();
  
              params1.add(new BasicNameValuePair("me", "52"));
  
              JSONObject json = jParser.makeHttpRequest(url_daftar_rs1, "POST",
                      params1);
  
              try {
  
                  int success = json.getInt(TAG_SUCCESS);
                  Log.i("success :", "" + success);
  
                  if (success == 1) {
  
                      daftar_rs = json.getJSONArray(TAG_DAFTAR_RS);
                      for (int i = 0; i &lt; daftar_rs.length(); i++) {
                          JSONObject c = daftar_rs.getJSONObject(i);
                          String id_rs = c.getString(TAG_ID_RS);
                          String nama_rs = c.getString(TAG_NAMA_RS);
                          String link_image_rs = c.getString(TAG_LINK_IMAGE_RS);
  
                          HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                          map.put(TAG_ID_RS, id_rs);
                          map.put(TAG_NAMA_RS, nama_rs);
                          map.put(TAG_LINK_IMAGE_RS,
                                  "http://192.168.137.1/www/Android_Login_Secure/Images/upload/big/"
                                          + link_image_rs);
  
                          arraylist.add(map);
  
                      }
                  } else {
  
                  }
              } catch (Exception e) {
                  Log.e("Error", "COnnection:" + e.toString());
              }
              return null;
          }
  
          protected void onPostExecute(String file_url) {
              pDialog.dismiss();
  
  
  
          ListView listUserOnline = (ListView) findViewById(R.id.listOfUserOnline);
  
  
              adapterFirends = new ListAdapterUserOnile(EchangingMessage.this,
                      arraylist);
  
              listUserOnline.setAdapter(adapterFirends);
  
          }
      }
  
  }
  </code></pre>
  
  <p>I created this  app but when I click the top button I got this error 
   and this is the popup window  , and this is connect with php </p>
  
  <p>please how to handle this error what should I do ?</p>
  
  <p><img src="https://i.stack.imgur.com/gnnMp.png" alt="enter image description here"></p>
  
  <p>and this my EchangeMesssage.xml file </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="wrap_content"
      android:layout_height="wrap_content"
     android:background="@drawable/backgroudnformain"
      android:orientation="vertical" &gt;
      &lt;LinearLayout 
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:background="#99cc00"
          android:gravity="center"
          &gt;
  
          &lt;ImageView
              android:id="@+id/userOnline"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:src="@drawable/userimages" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/bottom_write_bar"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:background="@drawable/message_bar" &gt;
  
          &lt;EditText
              android:id="@+id/messageToSend"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_margin="5sp"
              android:layout_weight="1"
              android:background="@drawable/message_field"
              android:hint="Write here"
              android:padding="5sp"
              android:textColor="@color/textFieldColor" /&gt;
  
          &lt;Button
              android:id="@+id/sendButton"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:layout_margin="5sp"
              android:background="@drawable/send_button"
              android:onClick="sendMessage"
              android:paddingBottom="5sp"
              android:paddingLeft="15sp"
              android:paddingRight="15sp"
              android:paddingTop="5sp"
              android:text="Send"
              android:textColor="#FFF" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;ListView
          android:id="@+id/listSend"
          android:layout_width="fill_parent"
          android:layout_height="0dip"
          android:layout_marginTop="40dp"
          android:layout_above="@id/bottom_write_bar"
          android:layout_alignParentTop="true"
          android:cacheColorHint="#00000000"
          android:divider="#00000000"
          android:stackFromBottom="true"
          android:clickable="false"
          android:scrollbars="none"
           android:transcriptMode="alwaysScroll"
          android:listSelector="#00000000" &gt;
      &lt;/ListView&gt;
  
  
          &lt;TextView
              android:id="@+id/you_id"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Large Text"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:visibility="gone" /&gt;
  
          &lt;TextView
              android:id="@+id/me_id"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Large Text"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:visibility="gone" /&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and this is my popup.xml file :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@drawable/windowwww"
      android:orientation="vertical" &gt;
  
      &lt;Button
          android:id="@+id/dismiss"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="240dp"
          android:layout_marginTop="60dp"
          android:text="EXIT" /&gt;
  
      &lt;ListView
          android:id="@+id/listOfUserOnline"
          android:layout_width="match_parent"
          android:layout_height="330dp"
          android:layout_marginTop="30dp" &gt;
      &lt;/ListView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And this is my ListAdapter class :</p>
  
  <pre><code>public class ListAdapterUserOnile extends BaseAdapter {
  
      public String POST_TEXT;
      private Activity activity;
      private ArrayList&lt;HashMap&lt;String, String&gt;&gt; data;
      private static LayoutInflater inflater = null;
      public ImageLoader imageLoader;
      public final static String you_id=null;
  
      int count = 0; 
  
      public ListAdapterUserOnile(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() {
          // TODO Auto-generated method stub
          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.item_list_friends, null);
          }
  
          TextView name_rs = (TextView) vi.findViewById(R.id.myFriendUsername);
  
  
          ImageView thumb_image = (ImageView) vi
                  .findViewById(R.id.myFriendImage);
  
  
          TextView status = (TextView) vi.findViewById(R.id.status);
  
          ImageView on=(ImageView) vi.findViewById(R.id.online);
          ImageView off=(ImageView) vi.findViewById(R.id.offline);
  
          HashMap&lt;String, String&gt; daftar_rs = new HashMap&lt;String, String&gt;();
          daftar_rs = data.get(position);
          final int getPosition = position;
          name_rs.setText(daftar_rs.get(EchangingMessage.TAG_NAMA_RS));
  
  
  
  
  
  
  
          Log.e("IMage", daftar_rs.get(EchangingMessage.TAG_LINK_IMAGE_RS));
  
          imageLoader.DisplayImage(daftar_rs.get(EchangingMessage.TAG_LINK_IMAGE_RS),
                  thumb_image);
  
          if (status.getText().toString().equals("1")) {
  
              off.setVisibility(View.INVISIBLE);
              on.setVisibility(View.VISIBLE);
              status.setVisibility(View.INVISIBLE);
  
          }
          else
          { 
              off.setVisibility(View.VISIBLE);
              on.setVisibility(View.INVISIBLE);
              status.setVisibility(View.INVISIBLE);
          }
  
  
  
  
          vi.setOnClickListener(new OnClickListener() {
  
  
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  Toast.makeText(
                          activity,
                          "  You click"
                                  + data.get(getPosition)
                                          .get(MyFriends.TAG_ID_RS).toString(),
                          Toast.LENGTH_SHORT).show();
  
                  Intent i=new Intent(activity,EchangingMessage.class);
                  i.putExtra(you_id, data.get(getPosition)
                                          .get(MyFriends.TAG_ID_RS).toString());
                  activity.startActivity(i);
  
  
              }
          });
  
          return vi;
  
      }
  
  }
  </code></pre>