title: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference - Android
body: <p>I am new to Android, i have been trying JSON parsing using PHP and MYSQL, when i run app got this error.</p>
  
  <p><strong>Error:</strong></p>
  
  <blockquote>
    <p>FATAL EXCEPTION: main
                      Process: com.example.admin.androidjsonparsing, PID: 13399
                      java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                          at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
                          at org.json.JSONTokener.nextValue(JSONTokener.java:94)
                          at org.json.JSONArray.(JSONArray.java:92)
                          at org.json.JSONArray.(JSONArray.java:108)
                          at com.example.admin.androidjsonparsing.MainActivity.loadIntoListView(MainActivity.java:79)
                          at com.example.admin.androidjsonparsing.MainActivity.access$000(MainActivity.java:22)
                          at com.example.admin.androidjsonparsing.MainActivity$1GetJSON.onPostExecute(MainActivity.java:51)
                          at com.example.admin.androidjsonparsing.MainActivity$1GetJSON.onPostExecute(MainActivity.java:38)
                          at android.os.AsyncTask.finish(AsyncTask.java:695)
                          at android.os.AsyncTask.access$600(AsyncTask.java:180)
                          at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
                          at android.os.Handler.dispatchMessage(Handler.java:106)
                          at android.os.Looper.loop(Looper.java:193)
                          at android.app.ActivityThread.main(ActivityThread.java:6669)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)</p>
  </blockquote>
  
  <p>Here the <strong>code:</strong></p>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.example.admin.androidjsonparsing;
  
  import android.os.AsyncTask;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.Toast;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  public class MainActivity extends AppCompatActivity {
  
  ListView listView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      listView = (ListView) findViewById(R.id.listView);
      getJSON("http://192.168.1.4/AndroidJSONParsing/getdata.php");
  }
  
  
  private void getJSON(final String urlWebService) {
  
      class GetJSON extends AsyncTask&lt;Void, Void, String&gt; {
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
          }
  
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
              try {
                  loadIntoListView(s);
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }
  
          @Override
          protected String doInBackground(Void... voids) {
              try {
                  URL url = new URL(urlWebService);
                  HttpURLConnection con = (HttpURLConnection) url.openConnection();
                  StringBuilder sb = new StringBuilder();
                  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                  String json;
                  while ((json = bufferedReader.readLine()) != null) {
                      sb.append(json + "\n");
                  }
                  return sb.toString().trim();
              } catch (Exception e) {
                  return null;
              }
          }
      }
      GetJSON getJSON = new GetJSON();
      getJSON.execute();
  }
  
  private void loadIntoListView(String json) throws JSONException {
      JSONArray jsonArray = new JSONArray(json);
      String[] heroes = new String[jsonArray.length()];
      for (int i = 0; i &lt; jsonArray.length(); i++) {
          JSONObject obj = jsonArray.getJSONObject(i);
          heroes[i] = obj.getString("name");
      }
      ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, heroes);
      listView.setAdapter(arrayAdapter);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.admin.androidjsonparsing.MainActivity"&gt;
  
  
  &lt;!-- this is our listview where we will display the fetched data --&gt;
  &lt;ListView
      android:id="@+id/listView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentTop="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>