title: Checking to see if I'm connected to internet first but my app is giving me an error message
body: <p>I'm checking to see if my app first has an internet connection. If it does, I want to create a toast message with it saying "Connected" and execute my task. If it's not connected I want the toast message to say "Not Connected". </p>
  
  <p>Here's my problem, when I'm connected to the internet my app will state "Connected" but the app soon crashes. When my app isn't connected to the internet it will state "Not Connected" (which is great because at least that part if working fine). My question is, how will I get my app not to crash? Below is my syntax and logcat error information. What do you guys think? </p>
  
  <pre><code>package com.example.connectexternaldatabase;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.support.v7.app.ActionBarActivity;
  import android.content.Context;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends ActionBarActivity {
  
      private ListView getAllCustomerListView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          getAllCustomerListView = (ListView) findViewById(R.id.getAllCustomerListView);
  
  
          isThisDeviceConnectedToTheInternet();
      }
  
      public void isThisDeviceConnectedToTheInternet(){
  
          ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
          if(networkInfo !=null &amp;&amp; networkInfo.isConnected()){
              Toast.makeText(getBaseContext(), "Connected", Toast.LENGTH_SHORT).show();
              new GetAllCustomerTask().execute(new APIConnector());
          } else {
              Toast.makeText(getBaseContext(), "Not Connected", Toast.LENGTH_SHORT).show();
          }
  
      }
  
  
      public void setListAdapter(JSONArray jsonArray){
          getAllCustomerListView.setAdapter(new GetAllCustomerListViewAdapter(jsonArray, MainActivity.this));
      }
  
      private class GetAllCustomerTask extends AsyncTask&lt;APIConnector, Long, JSONArray&gt;{
  
          @Override
          protected JSONArray doInBackground(APIConnector... params) {
              // executed on the background thread
              return params[0].GetAllCustomers();
          }
  
          @Override
          protected void onPostExecute(JSONArray jsonArray) {
              setListAdapter(jsonArray);
          }
  
  
  
      }
  
  }
  </code></pre>
  
  <p>Logcat Information</p>
  
  <pre><code>01-01 16:51:47.199: E/AndroidRuntime(4633): FATAL EXCEPTION: main
  01-01 16:51:47.199: E/AndroidRuntime(4633): java.lang.NullPointerException
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at com.example.connectexternaldatabase.GetAllCustomerListViewAdapter.getView(GetAllCustomerListViewAdapter.java:75)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.AbsListView.obtainView(AbsListView.java:1430)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.ListView.onMeasure(ListView.java:1127)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewRoot.performTraversals(ViewRoot.java:841)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1861)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.os.Handler.dispatchMessage(Handler.java:99)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.os.Looper.loop(Looper.java:130)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.app.ActivityThread.main(ActivityThread.java:3683)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at java.lang.reflect.Method.invoke(Method.java:507)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
  01-01 16:51:47.199: E/AndroidRuntime(4633):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>GetAllCustomerListViewAdapter</p>
  
  <pre><code>package com.example.connectexternaldatabase;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.app.Activity;
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  public class GetAllCustomerListViewAdapter extends BaseAdapter {
  
      private JSONArray dataArray;
      private Activity activity;
      private static LayoutInflater inflater = null;
  
      public GetAllCustomerListViewAdapter(JSONArray jsonArray, Activity a){
          this.dataArray = jsonArray;
          this.activity = a;
  
          inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      @Override
      public int getCount() {
          return this.dataArray.length();
      }
  
      @Override
      public Object getItem(int position) {
          return position;
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          // set up convert View, if it's null 
          ListCell cell;
          if(convertView == null){
              convertView = inflater.inflate(R.layout.get_all_customer_list_view_cell, null);
              cell = new ListCell();
  
              cell.fullName = (TextView) convertView.findViewById(R.id.customer_name);
              cell.color = (TextView) convertView.findViewById(R.id.customer_favorite_color);
              cell.facebook = (TextView) convertView.findViewById(R.id.customer_facebook);
              cell.twitter = (TextView) convertView.findViewWithTag(R.id.customer_twitter);
              cell.instagram = (TextView) convertView.findViewWithTag(R.id.customer_instagram);
              cell.mobile = (ImageView) convertView.findViewById(R.id.customer_mobile);
  
              convertView.setTag(cell);
  
          } else {
              cell = (ListCell) convertView.getTag();
          }
  
          try{
  
              JSONObject jsonObject = this.dataArray.getJSONObject(position);
              cell.fullName.setText(jsonObject.get("name")+" "+jsonObject.getString("last_name"));
  
  
              cell.color.setText("Favorite Color: "+jsonObject.getString("fav_color"));
              cell.facebook.setText("Facebook: "+ jsonObject.getString("facebook"));
              cell.twitter.setText("Twitter: "+ jsonObject.getString("twitter"));
              cell.instagram.setText("Instagram: "+ jsonObject.getString("instagram"));
  
              String mobile = jsonObject.getString("instagram");
              if(mobile.equals("instagram")){
                  cell.mobile.setImageResource(R.drawable.ic_launcher);
              }
  
          } catch (JSONException e){
              e.printStackTrace();
          }
  
  
          return convertView;
      }
  
      private class ListCell{
          private TextView fullName;
          private TextView color;
          private TextView facebook;
          private TextView twitter;
          private TextView instagram;
          private ImageView mobile;
      }
  
  }
  </code></pre>