title: How to fix FATAL EXCEPTION: AsyncTask #1 doinbackground?
body: <p>I'm new to Android programming and I can't pinpoint which triggers the error. Can someone please explain to me the error and how to get rid of it?</p>
  
  <p>I have this errors:</p>
  
  <pre><code>09-16 10:09:05.529: E/AndroidRuntime(26221): FATAL EXCEPTION: AsyncTask #1
  09-16 10:09:05.529: E/AndroidRuntime(26221): java.lang.RuntimeException: An error occured while executing doInBackground()
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at java.lang.Thread.run(Thread.java:841)
  09-16 10:09:05.529: E/AndroidRuntime(26221): Caused by: java.lang.NullPointerException
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at com.databaseaar.Login_new$AttemptLogin.doInBackground(Login_new.java:82)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at com.databaseaar.Login_new$AttemptLogin.doInBackground(Login_new.java:1)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  09-16 10:09:05.529: E/AndroidRuntime(26221):    ... 4 more
  09-16 10:09:06.459: E/WindowManager(26221): Activity com.databaseaar.Login_new has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42c25fb0 V.E..... R......D 0,0-684,192} that was originally added here
  09-16 10:09:06.459: E/WindowManager(26221): android.view.WindowLeaked: Activity com.databaseaar.Login_new has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42c25fb0 V.E..... R......D 0,0-684,192} that was originally added here
  09-16 10:09:06.459: E/WindowManager(26221):     at android.view.ViewRootImpl.&lt;init&gt;(ViewRootImpl.java:452)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.app.Dialog.show(Dialog.java:287)
  09-16 10:09:06.459: E/WindowManager(26221):     at com.databaseaar.Login_new$AttemptLogin.onPreExecute(Login_new.java:66)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.os.AsyncTask.execute(AsyncTask.java:534)
  09-16 10:09:06.459: E/WindowManager(26221):     at com.databaseaar.Login_new$1.onClick(Login_new.java:49)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.view.View.performClick(View.java:4475)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.view.View$PerformClick.run(View.java:18786)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.os.Handler.handleCallback(Handler.java:730)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.os.Handler.dispatchMessage(Handler.java:92)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.os.Looper.loop(Looper.java:137)
  09-16 10:09:06.459: E/WindowManager(26221):     at android.app.ActivityThread.main(ActivityThread.java:5419)
  09-16 10:09:06.459: E/WindowManager(26221):     at java.lang.reflect.Method.invokeNative(Native Method)
  09-16 10:09:06.459: E/WindowManager(26221):     at java.lang.reflect.Method.invoke(Method.java:525)
  09-16 10:09:06.459: E/WindowManager(26221):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
  09-16 10:09:06.459: E/WindowManager(26221):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
  09-16 10:09:06.459: E/WindowManager(26221):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>And this is my code for the class Login_new and asynctask:</p>
  
  <pre><code>package com.databaseaar;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.DialogInterface;
  import android.content.DialogInterface.OnClickListener;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  
       public class Login_new extends Activity implements OnClickListener{
          private EditText user, pass;
          private Button bLogin;
          private ProgressDialog pDialog;
  
          JSONParser jsonParser = new JSONParser(); 
  
          private static final String LOGIN_URL = "http://xxxx.com/login_new2.php"; 
          private static final String TAG_SUCCESS = "success"; 
          private static final String TAG_MESSAGE = "message";
          public static final String KEY_USERNAME ="username";
  
          protected void onCreate(Bundle savedInstanceState) { 
              super.onCreate(savedInstanceState); 
              setContentView(R.layout.login2);
              user = (EditText)findViewById(R.id.ET_email); 
              pass = (EditText)findViewById(R.id.ET_password);
              bLogin = (Button)findViewById(R.id.btnLogin); 
              //bLogin.setOnClickListener(Login_new.this); 
  
          bLogin.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
              // TODO Auto-generated method stub 
              switch (v.getId()) { 
              case R.id.btnLogin:
                  new AttemptLogin().execute();
              default:
                  break;
              }}
              });
          }
  
          class AttemptLogin extends AsyncTask&lt;String, String, String&gt; {
              // public static final String KEY_USERNAME = null;
              boolean failure = false;
  
               protected void onPreExecute() {
                   super.onPreExecute(); 
                   pDialog = new ProgressDialog(Login_new.this); 
                   pDialog.setMessage("Attempting for login..."); 
                   pDialog.setIndeterminate(false); 
                   pDialog.setCancelable(true); 
                   pDialog.show(); 
               }
  
              @Override
              protected String doInBackground(String... args) {
                  int success;
                  String username = user.getText().toString(); 
                  String password = pass.getText().toString();
                  try {
                      List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); 
                      params.add(new BasicNameValuePair("username", username)); 
                      params.add(new BasicNameValuePair("password", password));
  
                       Log.d("request!", "starting");
  
                       JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
                       Log.d("Login attempt", json.toString());
  
                      // success tag for json 
                       success = json.getInt(TAG_SUCCESS); 
                       if (success == 1) { 
                           Log.d("Successfully Login!", json.toString()); 
  
                       Intent ii = new Intent(Login_new.this,MainActivity.class);
                       ii.putExtra(KEY_USERNAME, username);
                       finish();
                       startActivity(ii);
  
                       return json.getString(TAG_MESSAGE);
                       }else {
  
                         return json.getString(TAG_MESSAGE); 
                     }
                     } catch (JSONException e) {
                         e.printStackTrace();
                  }
                     return null; 
          }
  
          protected void onPostExecute(String message) {
              pDialog.dismiss(); 
              if (message != null){ 
                  Toast.makeText(Login_new.this, message, Toast.LENGTH_LONG).show(); 
                  } 
              } }}
  </code></pre>
  
  <p>PHP code:</p>
  
  <pre><code>       &lt;?php
   $con=mysqli_connect("xxxx","xxxx","xxxx","xxxx");
  
    $password=$_POST["password"];
    $username=$_POST["username"];
  
  if (!empty($_POST)) { 
  if (empty($_POST['username']) || empty($_POST['password'])) {
  $response["success"] = 0;
  $response["message"] = "One or both of the fields are empty .";
  }
  $query = " SELECT * FROM login WHERE username = '$username'and password='$password'";
  
  $sql1=mysql_query($query);
  $row = mysql_fetch_array($sql1);
  
  if (!empty($row)) {
  $response["success"] = 1;
   $response["message"] = "You have been sucessfully login";
   die(json_encode($response));
   }else{
   $response["success"] = 0;
   $response["message"] = "invalid username or password ";
   die(json_encode($response));
   }}
    else{
    $response["success"] = 0;
    $response["message"] = " One or both of the fields are empty ";
      die(json_encode($response));
      }
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>My JSON httpRequest</p>
  
  <pre><code>public JSONObject makeHttpRequest(String url, String method, List&lt;NameValuePair&gt; params) {
      // Making HTTP Request
      try {
          // check for request method
          if (method == "POST") {
              // request method is POST
              // defaultHTTPClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);
              httpPost.setEntity(new UrlEncodedFormEntity(params));
  
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
  
          } else if (method == "GET") {
  
  
              DefaultHttpClient httpClient = new DefaultHttpClient();
              String paramString = URLEncodedUtils.format(params, "utf-8");
              url += "?" + paramString;
              HttpGet httpGet = new HttpGet(url);
  
              HttpResponse httpResponse = httpClient.execute(httpGet);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
          }
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
          is.close();
          json = sb.toString();
      } catch (Exception e) {
          Log.e("Buffer error", "Error converting result" + e.toString());
      }
  
      // try parse the string to a JSON object
      try {
          jsonObj = new JSONObject(json);
      } catch (JSONException e) {
          Log.e("JSON Parser", "Error parsing data " + e.toString());
      }
  
      return jsonObj;
  }
  </code></pre>
  
  <p>New error log cat:</p>
  
  <pre><code>    09-08 08:30:14.854: E/Response(2297): &lt;br /&gt;
  09-08 08:30:14.854: E/Response(2297): &lt;b&gt;Deprecated&lt;/b&gt;:  mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in &lt;b&gt;/home/u747670603/public_html/login_new2.php&lt;/b&gt; on line &lt;b&gt;14&lt;/b&gt;&lt;br /&gt;
  09-08 08:30:14.854: E/Response(2297): &lt;br /&gt;
  09-08 08:30:14.854: E/Response(2297): &lt;b&gt;Warning&lt;/b&gt;:  mysql_query(): Access denied for user 'u747670603'@'10.2.1.25' (using password: NO) in &lt;b&gt;/home/u747670603/public_html/login_new2.php&lt;/b&gt; on line &lt;b&gt;14&lt;/b&gt;&lt;br /&gt;
  09-08 08:30:14.854: E/Response(2297): &lt;br /&gt;
  09-08 08:30:14.854: E/Response(2297): &lt;b&gt;Warning&lt;/b&gt;:  mysql_query(): A link to the server could not be established in &lt;b&gt;/home/u747670603/public_html/login_new2.php&lt;/b&gt; on line &lt;b&gt;14&lt;/b&gt;&lt;br /&gt;
  09-08 08:30:14.854: E/Response(2297): &lt;br /&gt;
  09-08 08:30:14.854: E/Response(2297): &lt;b&gt;Warning&lt;/b&gt;:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in &lt;b&gt;/home/u747670603/public_html/login_new2.php&lt;/b&gt; on line &lt;b&gt;15&lt;/b&gt;&lt;br /&gt;
  09-08 08:30:14.854: E/Response(2297): {"success":0,"message":"invalid username or password "}
  </code></pre>