title: Getting java.lang.NullPointerException running android app
body: <p>I don't know what is going on, I can't run my app. What it does is take some information data from the API and list it in a listview, I think something is wrong  with my ClienteActivity, but I don't know what.</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android/com.android.MainActivity}: java.lang.NullPointerException
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
              at android.app.ActivityThread.access$800(ActivityThread.java:139)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5086)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
              at dalvik.system.NativeStart.main(Native Method)
       **Caused by: java.lang.NullPointerException
              at com.android.MainActivity.onCreate(MainActivity.java:40)**
              at android.app.Activity.performCreate(Activity.java:5248)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
              at android.app.ActivityThread.access$800(ActivityThread.java:139)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5086)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>The MainActivity:</p>
  
  <pre><code>public class MainActivity extends SherlockFragmentActivity {
  
      private ViewPager mViewPager;
      private TabsAdapter mTabsAdapter;
      private ActionBar barra;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
  
          mViewPager = new ViewPager(this);
          mViewPager.setId(R.id.pager);
          setContentView(mViewPager);
  
  
          mTabsAdapter = new TabsAdapter(this, mViewPager);
          **mTabsAdapter.addTab(barra.newTab().setIcon(R.drawable.clientes).setText(" Clientes"), ClienteActivity.class, null);**
  
  
          barra = getSupportActionBar();
          barra.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
  
  
  
      }
      private static final int REFRESH = 1;
      private static final int SEARCH = 2;
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getSupportMenuInflater();
          inflater.inflate(R.menu.main, menu);
  
          menu.add(0, REFRESH, 0, "Refresh")
                  .setIcon(R.drawable.ic_action_view_as_grid)
                  .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  
  
          menu.add(0, SEARCH, 0, "Search")
                  .setIcon(R.drawable.ic_action_search)
                  .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case REFRESH:
  
                  return true;
              case SEARCH:
                  // Do search
                  return true;
              case R.id.add_item:
                  this.dispatchSync();
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
  
  
  
  
      private void dispatchSync() {
  
          SincronizarTask mSincronizarTask = new SincronizarTask(this);
          mSincronizarTask.inicia();
  
      }
  
  
  }
  </code></pre>
  
  <p>The ClienteActivity:</p>
  
  <pre><code>public class ClienteActivity extends SherlockFragment {
  
  
      public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.fragment_cliente, container, false);
  
  
          try {
  
  
              ClienteViewAdapter ad = new ClienteViewAdapter(this.getActivity(), R.layout.fragment_cliente_item);
              ListView lv = (ListView) view.findViewById(R.id.listaClientes);
  
              lv.setVerticalFadingEdgeEnabled(true);
              lv.setVerticalScrollBarEnabled(true);
  
              lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
                  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
                     // TextView clienteId = (TextView) view.findViewById(R.id.clienteId);
                      TextView clienteNome = (TextView) view.findViewById(R.id.clienteNome);
                      TextView clienteTipoPessoa = (TextView) view.findViewById(R.id.clienteTipo);
  
                      Intent intent = new Intent(getActivity(), ContatosView.class);
                      //intent.putExtra("id_cliente", clienteId.getText()); // envia o id do cliente para a tela de detalhes
                      intent.putExtra("nome", clienteNome.getText());
                      intent.putExtra("tipo_pessoa", clienteTipoPessoa.getText());
  
                      startActivity(intent);
  
                  }
              });
  
              lv.setAdapter(ad);
  
          } catch (Exception e) {
              new AlertDialog.Builder(getActivity()).setTitle("Erro").setMessage(e.getMessage()).setNeutralButton("OK", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialogInterface, int i) {
  
                  }
              }).show();
          }
  
  
  
  
          return view;
      }
  
  
  
      private void gerarToast(CharSequence message) {
          int duration = Toast.LENGTH_LONG;
          Toast toast = Toast
                  .makeText(getActivity(), message, duration);
          toast.show();
      }
  
  }
  </code></pre>
  
  <p>The Adapter:</p>
  
  <pre><code>public class ClienteViewAdapter extends ArrayAdapter&lt;ClientesModel&gt; {
  
      private final LayoutInflater inflater;
  
      private final int resourceId;
  
      public ClienteViewAdapter(FragmentActivity context, int resource) {
          super(context, resource);
          this.inflater = LayoutInflater.from(context);
          this.resourceId = resource;
      }
  
      @Override
      public View getView(int position, View view, ViewGroup parent) {
  
          ClientesModel cliente = getItem(position);
  
          view = inflater.inflate(resourceId, parent, false);
  
          //TextView tvId = (TextView) view.findViewById(R.id.txtTitle);
          TextView tvNome = (TextView) view.findViewById(R.id.clienteNome);
          TextView tvTipo = (TextView) view.findViewById(R.id.clienteTipo);
  
          //tvId.setText(String.valueOf(cliente.ge()));
          tvNome.setText(cliente.getNome());
          tvTipo.setText(cliente.getTipo_pessoa());
  
          return view;
      }
  
  }
  </code></pre>
  
  <p>And the Parsed Json:</p>
  
  <pre><code>public class ClientRest {
      private RequestQueue mRequestQueue;
      private ArrayList&lt;ClientesModel&gt; arrNews ;
      public ArrayList&lt;ClientesContatosModel&gt; arrContatos ;
      private Activity context;
  
  
      public ClientRest(Activity ctx) {
          this.context = ctx;
      }
  
      public interface ClientesRestListener {
          public void clientesReceived(List&lt;ClientesModel&gt; clientes);
      }
  
      public final void getClientes(final ClientesRestListener listener) {
  
          String url = "http://192.168.1.1/api/api/client/index.php";
  
          // filtros
  
          final ProgressDialog pDialog = new ProgressDialog(this.context);
          pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          pDialog.setTitle("Sincronizando");
          pDialog.setMessage("Aguarde...");
          pDialog.show();
          arrNews = new ArrayList&lt;ClientesModel&gt;();
          arrContatos = new ArrayList&lt;ClientesContatosModel&gt;();
  
          mRequestQueue = Volley.newRequestQueue(this.context);
  
          JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener&lt;JSONObject&gt;() {
  
              @Override
              public void onResponse(JSONObject response) {
                  List&lt;ClientesModel&gt; clientes = null;
                  try {
                      clientes = parseJSON(response);
                      pDialog.hide();
                  } catch (JSONException e) {
                      e.printStackTrace();
  
                  }
                  listener.clientesReceived(clientes);
              }
  
          }, new Response.ErrorListener() {
  
              @Override
              public void onErrorResponse(VolleyError error) {
                  if (pDialog.isShowing()) {
                      pDialog.hide();
                  }
                  error.printStackTrace();
              }
  
          }
          ) 
  
          mRequestQueue.add(mJsonObjectRequest);
      }
  
      private List&lt;ClientesModel&gt; parseJSON(JSONObject json) throws JSONException {
          try {
  
  
              JSONArray dados = json.getJSONArray("dados");
  
              for (int i = 0; i &lt; dados.length(); i++) {
  
                  JSONObject item = dados.getJSONObject(i);
  
  
                  ClientesModel mClientesModel = new ClientesModel();
                  ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();
  
                  /* cadastra os dados necessários no objeto no modelo */
                  mClientesModel.setId(item.optInt("id"));
                  mClientesModel.setNome(item.optString("nome"));
                  mClientesModel.setTipo_pessoa(item.optString("tipo_pessoa"));
                  mClientesModel.setInformacoes_adicionais(item.optString("informacoes_adicionais"));
                  mClientesModel.setCpf(item.optString("cpf"));
                  mClientesModel.setCnpj(item.optString("cnpj"));
                  JSONArray contatos = item.getJSONArray("contatos");
                  for (int j = 0; j &lt; contatos.length(); j++) {
                      JSONObject data = contatos.getJSONObject(j);
  
                      mClientesContatoModel.setNome(data.optString("nome"));
  
                      mClientesContatoModel.setCargo(data.optString("cargo"));
  
                  }
  
                  arrNews.add(mClientesModel);
                  arrContatos.add(mClientesContatoModel);
              }
  
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          return arrNews;
      }
  
  
  }
  </code></pre>