title: I cannot access a Database with SQLite in Android Studio
body: <p>I'm building a aplication but when i try write data in there, show the error in the log.</p>
  
  <p>Class ActCadContatos.java</p>
  
  <pre><code>package com.example.gabrielgomes.agendacontact;
  
  import android.database.SQLException;
  import android.database.sqlite.SQLiteDatabase;
  import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.*;
  import android.widget.*;
  
  import com.example.gabrielgomes.agendacontact.database.DataBase;
  import com.example.gabrielgomes.agendacontact.dominio.RepositorioContato;
  import com.example.gabrielgomes.agendacontact.dominio.entidades.Contato;
  
  import java.util.Date;
  
  public class ActCadContatos extends AppCompatActivity {
  
      private EditText edtNome;
      private EditText edtTelefone;
      private EditText edtEmail;
      private EditText edtEndereco;
      private EditText edtDatasEspeciais;
      private EditText edtGrupos;
  
      private Spinner spnTipoTelefone;
      private Spinner spnTipoEmail;
      private Spinner spnTipoEndereco;
      private Spinner spnDatasEspeciais;
  
      //ArrayAdapter for save the options of the Spinner
      private ArrayAdapter&lt;String&gt; adpTipoEmail;
      private ArrayAdapter&lt;String&gt; adpTipoTelefone;
      private ArrayAdapter&lt;String&gt; adpTipoEndereco;
      private ArrayAdapter&lt;String&gt; adpTipoDatasEspeciais;
  
      //Objects for connection database
      private DataBase dataBase;
      private SQLiteDatabase conn;
      private RepositorioContato repositorioContato;
      private Contato contato;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_act_cad_contatos);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                          .setAction("Action", null).show();
              }
          });
  
          edtNome = (EditText)findViewById(R.id.edtNome);
          edtTelefone = (EditText)findViewById(R.id.edtTelefone);
          edtEmail = (EditText)findViewById(R.id.edtEmail);
          edtEndereco = (EditText)findViewById(R.id.edtEndereco);
          edtDatasEspeciais = (EditText)findViewById(R.id.edtDatasEspeciais);
          edtGrupos = (EditText)findViewById(R.id.edtGrupos);
  
          spnTipoTelefone = (Spinner)findViewById(R.id.spnTipoTelefone);
          spnTipoEndereco = (Spinner)findViewById(R.id.spnTipoEndereco);
          spnTipoEmail = (Spinner)findViewById(R.id.spnTipoEmail);
          spnDatasEspeciais = (Spinner)findViewById(R.id.spnDatasEspeciais);
  
          //Recovering the ArrayAdapter's
          adpTipoEmail = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item);
          adpTipoEmail.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
          adpTipoEndereco = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item);
          adpTipoEndereco.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
          adpTipoTelefone = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item);
          adpTipoTelefone.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
          adpTipoDatasEspeciais = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item);
          adpTipoDatasEspeciais.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
          //Connect object kind Spinner in a ArrayAdapter
          spnTipoEmail.setAdapter(adpTipoEmail);
          spnTipoEndereco.setAdapter(adpTipoEndereco);
          spnTipoTelefone.setAdapter(adpTipoTelefone);
          spnDatasEspeciais.setAdapter(adpTipoDatasEspeciais);
  
          //Add option in the ArrayAdapter for put in the Spinner
          adpTipoEmail.add("Pessoal");
          adpTipoEmail.add("Trabalho");
          adpTipoEmail.add("Outros");
  
          adpTipoTelefone.add("Casa");
          adpTipoTelefone.add("Trabalho");
  
          adpTipoEndereco.add("Casa");
          adpTipoEndereco.add("Trabalho");
          adpTipoEndereco.add("Outros");
  
          adpTipoDatasEspeciais.add("Aniversário");
          adpTipoDatasEspeciais.add("Namoro");
          adpTipoDatasEspeciais.add("Casamento");
  
          //Connection to database
          try {
  
              //dataBase comes to Class "Database" that create and upgrade the tables
              dataBase = new DataBase(this);
              //conn comes SQLiteDatabase Class it's Android default
              conn = dataBase.getWritableDatabase();
              //repositorioContato is where we sent the datas for input the database
              RepositorioContato repositorioContato = new RepositorioContato(conn);
  
          } catch (SQLException e){
  
              AlertDialog.Builder dlg = new AlertDialog.Builder(this);
              dlg.setMessage("Error to connect: " + e.getMessage());
              dlg.setNeutralButton("OK", null);
              dlg.show();
  
          }
  
      }
  
      //Method for create the menu and connect This with menu.xml
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.menu_act_cad_contato, menu);
  
          return super.onCreateOptionsMenu(menu);
  
      }
  
      //Method for create the action menu for regitry
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
  
          switch (item.getItemId()){
  
              case R.id.mni_acao1:
  
                  //why?
                  if (contato == null){
  
                      inserir();
                  }
  
                  finish();
  
                  break;
              case R.id.mni_acao2:
                  break;
  
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      //Method for insert datas of the activity for the database altrought get and setter
      private void inserir(){
  
          try {
  
              contato = new Contato();
              contato.setNome(edtNome.getText().toString());
              contato.setTelefone(edtTelefone.getText().toString());
              contato.setTipoTelefone("");
              contato.setEmail(edtEmail.getText().toString());
              contato.setTipoEmail("");
              contato.setEndereco(edtEndereco.getText().toString());
              contato.setTipoEndereco("");
              Date date = new Date();
              contato.setDatasEspeciais(date);
              contato.setTipoDatasEspeciais("");
              contato.setGrupos(edtGrupos.getText().toString());
              repositorioContato.inserir(contato);
  
          } catch (SQLException e){
  
              AlertDialog.Builder dlg = new AlertDialog.Builder(this);
              dlg.setMessage("Error to insert the datas: " + e.getMessage());
              dlg.setNeutralButton("OK", null);
              dlg.show();
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>Database Class</p>
  
  <pre><code>    package com.example.gabrielgomes.agendacontact.database;
  
      import android.content.Context;
      import android.database.sqlite.*;
  
      /**
       * Created by Gabriel Gomes on 31/07/2016.
       */
  
  //Class responsable by create Database, SQLiteOpenHelper just be extends
  public class DataBase extends SQLiteOpenHelper {
  
      //Constructor
      public DataBase(Context context){
          //Context, database name, null, version of schema
          super(context, "AGENDA", null, 1);
  
      }
  
      //Create table
      @Override
      public void onCreate(SQLiteDatabase db) {
  
          db.execSQL(ScriptSQL.getCreateContato());
  
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
  
  
      }
  }
  </code></pre>
  
  <p>Script Class</p>
  
  <pre><code>package com.example.gabrielgomes.agendacontact.database;
  
  /**
   * Created by Gabriel Gomes on 31/07/2016.
   */
  
  //Class for script SQL
  public class ScriptSQL {
  
      //Static method, don't need instace for access it
      public static String getCreateContato(){
  
          StringBuilder sqlBuilder = new StringBuilder();
          sqlBuilder.append("CREATE TABLE IF NOT EXISTS CONTATO ( ");
          sqlBuilder.append("_id INTEGER NOT NULL ");
          sqlBuilder.append("PRIMARY KEY AUTOINCREMENT, ");
          //sqlBuilder.append("NOME VARCHAR (200), ");
          sqlBuilder.append("TELEFONE VARCHAR (14), ");
          sqlBuilder.append("TIPOTELEFONE VARCHAR (1), ");
          sqlBuilder.append("EMAIL VARCHAR (255), ");
          sqlBuilder.append("TIPOEMAIL VARCHAR (1), ");
          sqlBuilder.append("ENDERECO VARCHAR (255), ");
          sqlBuilder.append("TIPOENDERECO VARCHAR (1), ");
          sqlBuilder.append("DATASESPECIAIS DATE, ");
          sqlBuilder.append("TIPODATASESPECIAIS VARCHAR (1), ");
          sqlBuilder.append("GRUPOS VARCHAR (255) ");
          sqlBuilder.append(");");
  
          return sqlBuilder.toString();
  
      }
  
  }
  </code></pre>
  
  <p>Repositorio Class</p>
  
  <pre><code>package com.example.gabrielgomes.agendacontact.dominio;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.*;
  import android.widget.ArrayAdapter;
  
  import com.example.gabrielgomes.agendacontact.R;
  import com.example.gabrielgomes.agendacontact.dominio.entidades.Contato;
  
  /**
   * Created by Gabriel Gomes on 31/07/2016.
   */
  
  //Class responsible for realize get the data in the content view, consulting in the database, and return the object Array to fill ListView
  public class RepositorioContato {
  
      private SQLiteDatabase conn;
  
      //Constructor
      public RepositorioContato(SQLiteDatabase conn){
  
          this.conn = conn;
  
      }
  
      //Method for insert datas of the activity in the database
      public void inserir(Contato contato){
  
          ContentValues values = new ContentValues();
          values.put("NOME", contato.getNome());
          values.put("TELEFONE", contato.getTelefone());
          values.put("TIPOTELEFONE", contato.getTipoTelefone());
          values.put("EMAIL", contato.getEmail());
          values.put("TIPOEMAIL", contato.getTipoEmail());
          values.put("ENDERECO", contato.getEndereco());
          values.put("TIPOENDERECO", contato.getTipoEndereco());
          values.put("DATASESPECIAIS", contato.getDatasEspeciais().getTime());
          values.put("TIPODATASESPECIAIS", contato.getTipoDatasEspeciais());
          values.put("GRUPOS", contato.getGrupos());
          conn.insertOrThrow("CONTATO", null, values);
      }
      //Atention: atribute bellow is kind date, so it's need the .getTime() method.
  
      //Test for insert datas
      /*public void testInserirContatos(){
  
          for (int i=0; i&lt;10; i++){
  
              //For insert datas in the database
              ContentValues values = new ContentValues();
              values.put("TELEFONE", "5555555");
              //name table, null, values.put
              conn.insertOrThrow("CONTATO", null, values);
  
          }
  
      }*/
  
      //This method make the search and save all datas of the table on the Array
      public ArrayAdapter&lt;String&gt; buscaContatos(Context context){
  
          ArrayAdapter&lt;String&gt; adpContatos = new ArrayAdapter&lt;String&gt;(context, android.R.layout.simple_list_item_1);
  
          //Save all resgitry   //.query - Method for consulting: table name,
          Cursor cursor = conn.query("CONTATO", null, null, null, null, null, null);
  
          //If content of cursor have data more 0, fill ArrayAdapter with cursor data
          if (cursor.getCount() &gt; 0) {
  
              //Move cursor to first data of the Array
              cursor.moveToFirst();
  
              //Loop for take all registry if have it
              do {
                  //getString catch the index column
                  String telefone = cursor.getString(1);
                  adpContatos.add(telefone);
  
              } while (cursor.moveToNext());
  
          }
  
          return adpContatos;
  
      }
  
  }
  </code></pre>
  
  <p>content_act_cad_contato.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout 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"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="com.example.gabrielgomes.agendacontact.ActCadContatos"
      tools:showIn="@layout/activity_act_cad_contatos"
      android:orientation="vertical"&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/lnlCampoNome"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="@string/lbl_Nome"
              android:id="@+id/textView" /&gt;
  
          &lt;EditText
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:inputType="textPersonName"
              android:ems="10"
              android:id="@+id/edtNome" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/lnlCampoTelefone"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="@string/lbl_Telefone"
              android:id="@+id/textView2" /&gt;
  
          &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
  
              &lt;EditText
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:inputType="phone"
                  android:ems="10"
                  android:id="@+id/edtTelefone" /&gt;
  
              &lt;Spinner
                  android:layout_width="150dp"
                  android:layout_height="wrap_content"
                  android:id="@+id/spnTipoTelefone" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/lnlCampoEmail"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="@string/lbl_Email"
              android:id="@+id/textView3" /&gt;
  
          &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent" &gt;
  
              &lt;EditText
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:ems="10"
                  android:id="@+id/edtEmail"
                  android:inputType="textEmailAddress" /&gt;
  
              &lt;Spinner
                  android:layout_width="150dp"
                  android:layout_height="wrap_content"
                  android:id="@+id/spnTipoEmail" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/lnlCampoEndereco"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="@string/lbl_Endereco"
              android:id="@+id/textView4" /&gt;
  
          &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent" &gt;
  
              &lt;EditText
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:ems="10"
                  android:id="@+id/edtEndereco"
                  android:inputType="textPersonName" /&gt;
  
              &lt;Spinner
                  android:layout_width="150dp"
                  android:layout_height="wrap_content"
                  android:id="@+id/spnTipoEndereco" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/lnlCampoDatasEspeciais"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="@string/lbl_Datas_Especiais"
              android:id="@+id/textView5" /&gt;
  
          &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent" &gt;
  
              &lt;EditText
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:ems="10"
                  android:id="@+id/edtDatasEspeciais"
                  android:inputType="date" /&gt;
  
              &lt;Spinner
                  android:layout_width="150dp"
                  android:layout_height="wrap_content"
                  android:id="@+id/spnDatasEspeciais" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/lnlGrupo"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:text="@string/lbl_Grupos"
              android:id="@+id/textView6" /&gt;
  
          &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent" &gt;
  
              &lt;EditText
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:ems="10"
                  android:id="@+id/edtGrupos"
                  android:inputType="textPersonName" /&gt;
  
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>LOG:</p>
  
  <p>08-06 16:35:22.832 2268-2268/com.example.gabrielgomes.agendacontact E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: com.example.gabrielgomes.agendacontact, PID: 2268
                                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.gabrielgomes.agendacontact.dominio.RepositorioContato.inserir(com.example.gabrielgomes.agendacontact.dominio.entidades.Contato)' on a null object reference
                                                                                            at com.example.gabrielgomes.agendacontact.ActCadContatos.inserir(ActCadContatos.java:183)
                                                                                            at com.example.gabrielgomes.agendacontact.ActCadContatos.onOptionsItemSelected(ActCadContatos.java:152)
                                                                                            at android.app.Activity.onMenuItemSelected(Activity.java:2885)
                                                                                            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:404)
                                                                                            at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:179)
                                                                                            at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                                            at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                                            at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
                                                                                            at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:169)
                                                                                            at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
                                                                                            at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                                                                                            at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                                                            at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                                                                                            at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
                                                                                            at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
                                                                                            at android.widget.AdapterView.performItemClick(AdapterView.java:305)
                                                                                            at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
                                                                                            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
                                                                                            at android.widget.AbsListView$3.run(AbsListView.java:3860)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  08-06 16:35:25.308 2268-2268/com.example.gabrielgomes.agendacontact I/Process: Sending signal. PID: 2268 SIG: 9
  08-06 16:35:27.992 4214-4239/com.example.gabrielgomes.agendacontact D/OpenGLRenderer: endAllStagingAnimators on 0x7fe895d4a400 (RippleDrawable) with handle 0x7fe895c7f5a0
  08-06 16:36:56.285 4214-4222/com.example.gabrielgomes.agendacontact W/art: Suspending all threads took: 7.399ms
  08-06 16:38:36.537 4214-4222/com.example.gabrielgomes.agendacontact W/art: Suspending all threads took: 9.017ms
  08-06 16:43:31.304 4214-4222/com.example.gabrielgomes.agendacontact W/art: Suspending all threads took: 5.852ms
  08-06 16:43:37.818 4214-4222/com.example.gabrielgomes.agendacontact W/art: Suspending all threads took: 8.820ms</p>