title: SQLite Database is locked; getDatabaseLocked()
body: <p>The aim is to retrieve the car park names from the car park tables columns 'CPNAME' and put those rows of names in another class' arraylist which a spinner will display. To be honest Im not even sure if this way will even show the rows in the spinner...</p>
  
  <p>The problem at the moment is that when I go into the activity where the spinner accesses the arraylist that calls getCpnames(). I really cannot work out which part of my code is making other calls to the database to lock it other than getCpnames(). Thank you for the help in advance, I am pretty new to programming. </p>
  
  <p><strong>Error messages:</strong></p>
  
  <pre><code>Caused by: java.lang.NullPointerException
  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256)
  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
  atandroid.database.sqlite.SQLiteOpenHelper.getWritableDatabase
  (SQLiteOpenHelper.java:164)
  at com.example.parkangel.DbHelper.getCpnames(DbHelper.java:74)
  at com.example.parkangel.BookTicket.&lt;init&gt;(BookTicket.java:19)
  </code></pre>
  
  <p>*<em>My database class code: *</em></p>
  
  <pre><code>package com.example.parkangel;
  
  import java.util.ArrayList;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.SQLException;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  public class DbHelper extends SQLiteOpenHelper
  {
  private DbHelper dbHelper;
  private Context ourContext;
  private static DbHelper instance;
  private SQLiteDatabase ourDatabase;
  
  private static final String DATABASE_NAME = "CPDB.db";
  private static final String DATABASE_TABLE = "CPTable";
  private static final int DATABASE_VERSION = 1;
  
  public static final String KEY_ID = "_id";
  public static final String KEY_CPNAME = "cpname";
  public static final String KEY_COST = "cost";
  
      public DbHelper(Context context)
      {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
          // TODO Auto-generated constructor stub 
      }
  
      public static DbHelper getInstance(Context context)
      {
          if (instance == null)
          {
              instance = new DbHelper(context);   
          }
          return instance;
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) 
      {
          // TODO Auto-generated method stub
          db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                  KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                  KEY_CPNAME + " TEXT NOT NULL, " + KEY_COST + " TEXT 
                                      NOT NULL);");
  
          db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('1','Learning 
                      Resource Center','2');");
          db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('2','Park and 
                      Ride','1');");
          db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('3','de 
                      Havilland Campus','2');");
          db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('4','Multi 
                      Storey Building','2');");
          db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values 
                      ('5','Reception','2');");   
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
      {
          // TODO Auto-generated method stub
          db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
          onCreate(db);
      }
  
      public synchronized DbHelper open() throws SQLException
      {
          System.out.println ("running open");
          if(ourDatabase == null || !ourDatabase.isOpen())
          ourDatabase = getWritableDatabase();
          return this;
      }   
  
      public String[] getCpnames()
      {
          if(ourDatabase == null || !ourDatabase.isOpen())
          ourDatabase = getWritableDatabase();
          String[] columns = new String[] {KEY_ID, KEY_CPNAME, KEY_COST};
          Cursor  c = ourDatabase.query(DATABASE_TABLE, columns, KEY_CPNAME,     
                      null, null, null, null);
          ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
          if (c != null)
          {
              c.moveToFirst();
              do
              {
                  list.add(c.getString(0));
              }           
              while (c.moveToNext());
          }
          if (ourDatabase == null) System.out.println ("is null");
  
          return list.toArray(new String[]{});
      }   
  }
  </code></pre>
  
  <p><strong>This is the activity class where an araylist will store the rows retrieved rows:</strong></p>
  
  <pre><code>package com.example.parkangel;
  
  import android.app.Activity;
  import android.app.Dialog;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Spinner;
  import android.widget.TextView;
  
  public class BookTicket extends Activity implements OnClickListener{
  
  Spinner spinner, spinner2;
  String[] carParks = DbHelper.getInstance(this).getCpnames();
  </code></pre>