title: Cannot connect Android device to database using XAMPP
body: <p>I'm trying to connect an android device to a database using XAMPP</p>
  
  <p>MySQL and Apache is working properly</p>
  
  <p>This is <strong>UpdateDatabase.java</strong></p>
  
  <pre><code>public class UpdateDatabase extends ListActivity {
  
  private ProgressDialog pDialog;
  
  //192.168.1.254 - IP of the server(laptop)
  private static String url = "http://192.168.1.254:80/shopsmart/test.php";
  
  //JSON Node Names
  private static final String prod_list = "product";
  private static final String prod_id = "ProductID";
  private static final String prod_aisle = "Aisle";
  private static final String prod_category = "Category";
  private static final String prod_subcategory = "SubCategory";
  private static final String prod_name = "ProductName";
  private static final String prod_price = "Price";
  
  // Product JSON Array
  JSONArray products = null;
  
  // Hashmap for ListView
  ArrayList&lt;HashMap&lt;String, String&gt;&gt;productList;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.update_database);
  
      productList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
      ListView lv = getListView();
  
      // Calling async task to get json
      new GetDatabase().execute();
  }
  
  /**
   * Async task class to get json by making HTTP call
   * */
  private class GetDatabase extends AsyncTask&lt;Void, Void, Void&gt; {
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          // Showing progress dialog
          pDialog = new ProgressDialog(UpdateDatabase.this);
          pDialog.setMessage("Loading...");
          pDialog.setCancelable(false);
          pDialog.show();
  
      }
  
      @Override
      protected Void doInBackground(Void... arg0) {
          // Creating service handler class instance
          ServiceHandler sh = new ServiceHandler();
  
          // Making a request to url and getting response
          String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
  
          Log.e("Response: ", "&gt; " + jsonStr);
  
          //if (jsonStr != null){
              try{
                  JSONObject jsonObj = new JSONObject(jsonStr);
  
                  // Getting JSON Array node
                  products = jsonObj.getJSONArray(prod_list);
  
                  // Looping through all products
                  for(int x =0; x&lt;products.length();x++){
                      JSONObject p = products.getJSONObject(x);
  
                      String id = p.getString(prod_id);
                      String name = p.getString(prod_name);
                      String category = p.getString(prod_category);
                      String subcategory = p.getString(prod_subcategory);
                      String aisle = p.getString(prod_aisle);
                      String price = p.getString(prod_price);
  
                      // tmp hashmap for single product
                      HashMap&lt;String,String&gt; product = new HashMap&lt;String,String&gt;();
  
                      // adding each child node to HashMap key =&gt; value
                      product.put(prod_id, id);
                      product.put(prod_aisle, aisle);
                      product.put(prod_category, category);
                      product.put(prod_subcategory, subcategory);
                      product.put(prod_name, name);
                      product.put(prod_price, price);
  
                      // adding product to product list
                      productList.add(product);
                      Log.e("productList", "" + productList);
                  }
              }
              catch(JSONException e){
                  e.printStackTrace();
              }
          //}
          //else{
          //  Log.e("ServiceHandler","Couldn't get any data from the url");
          //}
  
          return null;
      }
  
  
      @Override
      protected void onPostExecute(Void result){
          super.onPostExecute(result);
          //Dismiss the progress dialog
          if(pDialog.isShowing())
              pDialog.dismiss();
  
          // updating parsed JSON data into Listview
          ListAdapter adapter = new SimpleAdapter(
              UpdateDatabase.this, productList,
              R.layout.update_database,new String[]{prod_name,prod_aisle,prod_category,prod_subcategory},
              new int[]{R.id.name,R.id.aisle,R.id.category,R.id.subcategory}
          );
  
          setListAdapter(adapter);
      }
  
  }
  
  }
  </code></pre>
  
  <p>In this portion of the code:</p>
  
  <pre><code>String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
  </code></pre>
  
  <p>jsonStr is equal to Null and a NullPointerException will occur;</p>
  
  <p>What should I do?</p>
  
  <p>Thanks in advance</p>
  
  <p>This is the ServiceHandler.java</p>
  
  <pre><code>public class ServiceHandler {
  
  static String response = null;
  public final static int GET = 1;
  public final static int POST = 2;
  
  public ServiceHandler() {
  
  }
  
  /**
   * Making service call
   * @url - url to make request
   * @method - http request method
   * */
  public String makeServiceCall(String url, int method) {
      return this.makeServiceCall(url, method, null);
  }
  
  /**
   * Making service call
   * @url - url to make request
   * @method - http request method
   * @params - http request params
   * */
  public String makeServiceCall(String url, int method,
          List&lt;NameValuePair&gt; params) {
      try {
          // http client
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpEntity httpEntity = null;
          HttpResponse httpResponse = null;
  
          // Checking http request method type
          if (method == POST) {
              HttpPost httpPost = new HttpPost(url);
              // adding post params
              if (params != null) {
                  httpPost.setEntity(new UrlEncodedFormEntity(params));
              }
  
              httpResponse = httpClient.execute(httpPost);
  
          } else if (method == GET) {
              // appending params to url
              if (params != null) {
                  String paramString = URLEncodedUtils
                          .format(params, "utf-8");
                  url += "?" + paramString;
              }
              HttpGet httpGet = new HttpGet(url);
  
              httpResponse = httpClient.execute(httpGet);
  
          }
          httpEntity = httpResponse.getEntity();
          response = EntityUtils.toString(httpEntity);
  
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      return response;
  
  }
  }
  </code></pre>
  
  <p>This is also the Error Message in LogCat:</p>
  
  <pre><code>01-17 03:20:38.461: E/AndroidRuntime(21809): FATAL EXCEPTION: AsyncTask #1
  01-17 03:20:38.461: E/AndroidRuntime(21809): java.lang.RuntimeException: An error occured while executing doInBackground()
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at java.lang.Thread.run(Thread.java:838)
  01-17 03:20:38.461: E/AndroidRuntime(21809): Caused by: java.lang.NullPointerException
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at org.json.JSONTokener.nextValue(JSONTokener.java:94)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at org.json.JSONObject.&lt;init&gt;(JSONObject.java:154)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at org.json.JSONObject.&lt;init&gt;(JSONObject.java:171)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at com.example.shopsmart.UpdateDatabase$GetDatabase.doInBackground(UpdateDatabase.java:88)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at com.example.shopsmart.UpdateDatabase$GetDatabase.doInBackground(UpdateDatabase.java:1)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  01-17 03:20:38.461: E/AndroidRuntime(21809):    ... 4 more
  </code></pre>