title: NullpointerException using sqlite database on android
body: <p>I'am trying to insert playlist object into sqlite database with android but I get a NullPointerException when opening the database.
  Here is my PlaylistDbAdapter:</p>
  
  <pre><code>    import java.util.ArrayList;
      import java.util.List;
  
      import android.content.ContentValues;
      import android.content.Context;
      import android.database.Cursor;
      import android.database.SQLException;
      import android.database.sqlite.SQLiteDatabase;
      import android.database.sqlite.SQLiteOpenHelper;
  
      public class PlaylistDbAdapter{
  
  // All Static variables
  // Database Version
  private static final int DATABASE_VERSION = 2;
  
  // Database Name
  private static final String DATABASE_NAME = "playlistdb";
  
  // Contacts table name
  private static final String TABLE_NAME = "playlists";
  
  // Contacts Table Columns names
  private static final String KEY_ID = "id";
  private static final String KEY_NAME = "name";
  private DatabaseHelper dh;
   private SQLiteDatabase mDb;
  private final Context mCtx;
  
  private static class DatabaseHelper extends SQLiteOpenHelper{
  
  public DatabaseHelper(Context context) 
  {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
  // Creating Tables
  @Override
  public void onCreate(SQLiteDatabase db) {
      String CREATE_PLAYLISTS_TABLE =  "create table playlists (id integer primary key autoincrement, name text not null);";
      db.execSQL(CREATE_PLAYLISTS_TABLE);
  }
  
  // Upgrading database
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // Drop older table if existed
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  
      // Create tables again
      onCreate(db);
  }
  }
  
  
   public  PlaylistDbAdapter(Context ctx) {
          this.mCtx = ctx;
      }
   public PlaylistDbAdapter open() throws SQLException {
          dh = new DatabaseHelper(mCtx);
          mDb = dh.getWritableDatabase();
          return this;
      }
  
   public void close() {
          dh.close();
      }
  
  /**
   * All CRUD(Create, Read, Update, Delete) Operations
   * @return 
   */
  
  void addPlaylist(Playlist playlist) {
  
      ContentValues values = new ContentValues();
      values.put(KEY_NAME, playlist.getName()); // Playlist Name
  
      // Inserting Row
      mDb.insert(TABLE_NAME, null, values);
      mDb.close(); // Closing database connection
  }
  
  // Getting single playlist
  Playlist getPlaylist(int id) {
      SQLiteDatabase db = dh.getReadableDatabase();
  
      Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID,
              KEY_NAME}, KEY_ID + "=?",
              new String[] { String.valueOf(id) }, null, null, null);
      if (cursor != null)
          cursor.moveToFirst();
  
      Playlist playlist = new Playlist (Integer.parseInt(cursor.getString(0)),
              cursor.getString(1));
      // return playlist
      System.out.println(playlist._name);
      return playlist;
  }
  
  // Getting All Playlists
  public List&lt;Playlist&gt; getAllPlaylists() {
      List&lt;Playlist&gt; contactList = new ArrayList&lt;Playlist&gt;();
      // Select All Query
      String selectQuery = "SELECT  * FROM " + TABLE_NAME;
  
      SQLiteDatabase db = dh.getWritableDatabase();
      Cursor cursor = db.rawQuery(selectQuery, null);
  
      // looping through all rows and adding to list
      if (cursor.moveToFirst()) {
          do {
              Playlist playlist = new Playlist();
              playlist.setID(Integer.parseInt(cursor.getString(0)));
              playlist.setName(cursor.getString(1));
              // Adding playlist to list
              contactList.add(playlist);
          } while (cursor.moveToNext());
      }
  
      // return playlist list
      return contactList;
  }
  
  // Updating single playlist
  public int updatePlaylist(Playlist playlist) {
      SQLiteDatabase db = dh.getWritableDatabase();
  
      ContentValues values = new ContentValues();
      values.put(KEY_NAME, playlist.getName());
  
      // updating row
      return db.update(TABLE_NAME, values, KEY_ID + " = ?",
              new String[] { String.valueOf(playlist.getID()) });
  }
  
  // Deleting single playlist
  public void deletePlaylist(Playlist playlist) {
      SQLiteDatabase db = dh.getWritableDatabase();
      db.delete(TABLE_NAME, KEY_ID + " = ?",
              new String[] { String.valueOf(playlist.getID()) });
      db.close();
  }
  
  
  // Getting playlists Count
  public int getPlaylistCount() {
      String countQuery = "SELECT  * FROM " + TABLE_NAME;
      SQLiteDatabase db = dh.getReadableDatabase();
      Cursor cursor = db.rawQuery(countQuery, null);
      cursor.close();
      // return count
      return cursor.getCount();
  }
  
      }
  </code></pre>
  
  <p>this is my playlist object</p>
  
  <pre><code>    public class Playlist {
  
  
  //private variables
  int _id;
  String _name;
  
  // Empty constructor
  public Playlist(){
  
  }
  // constructor
  public Playlist(int id, String name){
      this._id = id;
      this._name = name;
  }
  
  // constructor
  public Playlist(String name){
      this._name = name;
  }
  // getting ID
  public int getID(){
      return this._id;
  }
  
  // setting id
  public void setID(int id){
      this._id = id;
  }
  
  // getting name
  public String getName(){
      return this._name;
  }
  
  // setting name
  public void setName(String name){
      this._name = name;
  }
  
      }
  </code></pre>
  
  <p>and this is AddPlaylist class in which I add the new playlist (using an an alert dialog in which the user chooses the name of the playlist).</p>
  
  <pre><code>    import android.os.Bundle;
  
      import android.text.InputType;
      import android.view.View;
      import android.widget.EditText;
      import android.widget.ImageButton;
      import android.widget.Toast;
      import android.app.Activity;
      import android.app.AlertDialog;
      import android.content.Context;
      import android.content.DialogInterface;
      import android.database.sqlite.SQLiteDatabase;
  
  
      public class AddPlaylist extends Activity{
  private ImageButton btnAddPlaylist;
  private String playlistName = "";
  public PlaylistDbAdapter dh;
  private SQLiteDatabase db;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);     
      setContentView(R.layout.add_playlist);
      btnAddPlaylist = (ImageButton) findViewById(R.id.btnAddPlaylist);
      btnAddPlaylist.setOnClickListener(new View.OnClickListener(){
  
          @Override
          public void onClick(View arg0) {
              popUpNewPlaylist();
          }
  
      });
  }
  public void popUpNewPlaylist()
  {   
  
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("Playlist name");
  
      // Set up the input
      final EditText input = new EditText(this);
      // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
      input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_TEXT);
      builder.setView(input);
  
      // Set up the buttons
      builder.setPositiveButton("Add", new DialogInterface.OnClickListener() { 
          @Override
          public void onClick(DialogInterface dialog, int which) {
              playlistName = input.getText().toString();
              Playlist tmpPLST = new Playlist();
              tmpPLST.setName(playlistName);
              dh.open();
              //dh.addPlaylist(tmpPLST);
              popupSuccessToast();
          }
      });
      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              dialog.cancel();
              popupCancelToast();
          }
      });
  
      builder.show();
  }
  
  public void popupSuccessToast()
  {
      Context context = getApplicationContext();
      CharSequence text = "Playlist "+playlistName+" Added";
      int duration = Toast.LENGTH_SHORT;
  
      Toast toast = Toast.makeText(context, text, duration);
      toast.show();
  }
  
  public void popupCancelToast()
  {
      Context context = getApplicationContext();
      CharSequence text = "Operation canceled";
      int duration = Toast.LENGTH_SHORT;
  
      Toast toast = Toast.makeText(context, text, duration);
      toast.show();
  }
  
  
      } 
  </code></pre>
  
  <p>my crash log</p>
  
  <pre><code>    04-27 18:43:34.151: E/AndroidRuntime(4338): FATAL EXCEPTION: main
      04-27 18:43:34.151: E/AndroidRuntime(4338): java.lang.NullPointerException
      04-27 18:43:34.151: E/AndroidRuntime(4338):     at                               ca.uqac.powerplayer.AddPlaylist$2.onClick(AddPlaylist.java:58)
      04-27 18:43:34.151: E/AndroidRuntime(4338):     at             com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java)
       04-27 18:43:34.151: E/AndroidRuntime(4338):    at    android.os.Handler.dispatchMessage(Handler.java)
       04-27 18:43:34.151: E/AndroidRuntime(4338):    at android.os.Looper.loop(Looper.java)
      04-27 18:43:34.151: E/AndroidRuntime(4338):     at android.app.ActivityThread.main(ActivityThread.java)
      04-27 18:43:34.151: E/AndroidRuntime(4338):     at java.lang.reflect.Method.invokeNative(Native Method)
      04-27 18:43:34.151: E/AndroidRuntime(4338):     at java.lang.reflect.Method.invoke(Method.java)
      04-27 18:43:34.151: E/AndroidRuntime(4338):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
      04-27 18:43:34.151: E/AndroidRuntime(4338):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
      04-27 18:43:34.151: E/AndroidRuntime(4338):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Thanks.</p>