1

i'm a new android developper and i'm getting a null pointer exception when i try to insert data into a sqlite database.

hope someone can help me.

Here's my code :

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String TABLE_ALBUM = "table_album";
private static final String COL_ID = "ID";
private static final String COL_ARTIST = "ARTIST";
private static final String COL_TITLE = "TITLE";
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_ALBUM
        + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COL_ARTIST + " TEXT NOT NULL, " + COL_TITLE + " TEXT NOT NULL);";

public DatabaseHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_BDD);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE " + TABLE_ALBUM + ";");
    onCreate(db);
}
}

Album_db.java

public class Album_db {

private static String DATABASE_NAME = "playlist.db";
private static final int VERSION_BDD = 1;
private static final String TABLE_ALBUM = "table_album";
private static final String COL_ID = "ID";
private static final String COL_ARTIST = "ARTIST";
private static final String COL_TITLE = "TITLE";    
private SQLiteDatabase db;
private DatabaseHelper oDB;

public Album_db(Context context) {

    oDB = new DatabaseHelper(context, DATABASE_NAME, null, VERSION_BDD);
}

public void open() {
    db = oDB.getWritableDatabase();
}

public void close() {
    oDB.close();
}

public SQLiteDatabase getBd() {
    return db;
}

public long insertAlbum(Album album) {
    ContentValues values = new ContentValues(); // hashmap
    values.put(COL_ARTIST, album.getArtist());
    values.put(COL_TITLE, album.getTitle());
    return db.insert(TABLE_ALBUM, null, values); // insert ds la base par le ContentValues
}

public int updateAlbum(int id, Album album) {
    ContentValues values = new ContentValues();
    values.put(COL_ARTIST, album.getArtist());
    values.put(COL_TITLE, album.getTitle());
    return db.update(TABLE_ALBUM, values, COL_ID + " = " + id,
            null);
}

public int removeAlbum(int id) {
    // Suppression BDD via id
    return db.delete(TABLE_ALBUM, COL_ID + " = " + id, null);
} 
public ArrayList<String> getAlbums() {
    ArrayList<String> output = new ArrayList<String>();

    String[] colonnesARecup = new String[] { "ARTIST", "TITLE" };

    Cursor cursorResults = db.query(TABLE_ALBUM, colonnesARecup,
            null, null, null, null, "ARTIST asc, TITLE asc", null);
    if (null != cursorResults) {
        if (cursorResults.moveToFirst()) {
            do {
                int columnIdxArtiste = cursorResults
                        .getColumnIndex("ARTIST");
                int columnIdxTitre = cursorResults.getColumnIndex("TITLE");
                String oItem = cursorResults.getString(columnIdxArtiste)
                        + " -  " + cursorResults.getString(columnIdxTitre);
                output.add(oItem);
            } while (cursorResults.moveToNext());
        }
    }
    return output;
}

PlayListActivity.java

public class PlayListActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */

private static final int CODE_ACTIVITY = 1;
Button btAdd;
Button btList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btAdd = (Button) findViewById(R.id.btAdd);
    btAdd.setOnClickListener(this);
    btList = (Button) findViewById(R.id.btTab2);
    btList.setOnClickListener(this);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == btAdd) {
        Album_db db = new Album_db(this);

        String artiste = ((EditText) this.findViewById(R.id.editArtist))
                .getText().toString();
        String titre = ((EditText) this.findViewById(R.id.editTitle))
                .getText().toString();

        Album album = new Album(artiste, titre);
        db.insertAlbum(album);
        updateList();
    }

    if (v == btList) {
        Intent oIntent = new Intent(PlayListActivity.this,
                ListActivity.class);
        startActivityForResult(oIntent, CODE_ACTIVITY);
        updateList();
    }
}

public void updateList() {
    Album_db db = new Album_db(this);
    ListView listeAlbum = (ListView) this.findViewById(R.id.albListView);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, db.getAlbums());
    listeAlbum.setAdapter(adapter);
}

Log :

    02-09 13:06:22.170: E/AndroidRuntime(531): FATAL EXCEPTION: main
02-09 13:06:22.170: E/AndroidRuntime(531): java.lang.NullPointerException
02-09 13:06:22.170: E/AndroidRuntime(531):  at test.themit.com.Album_db.insertAlbum(Album_db.java:42)
02-09 13:06:22.170: E/AndroidRuntime(531):  at test.themit.com.PlayListActivity.onClick(PlayListActivity.java:43)
02-09 13:06:22.170: E/AndroidRuntime(531):  at android.view.View.performClick(View.java:3511)
02-09 13:06:22.170: E/AndroidRuntime(531):  at android.view.View$PerformClick.run(View.java:14105)
02-09 13:06:22.170: E/AndroidRuntime(531):  at android.os.Handler.handleCallback(Handler.java:605)
02-09 13:06:22.170: E/AndroidRuntime(531):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-09 13:06:22.170: E/AndroidRuntime(531):  at android.os.Looper.loop(Looper.java:137)
02-09 13:06:22.170: E/AndroidRuntime(531):  at android.app.ActivityThread.main(ActivityThread.java:4424)
02-09 13:06:22.170: E/AndroidRuntime(531):  at java.lang.reflect.Method.invokeNative(Native Method)
02-09 13:06:22.170: E/AndroidRuntime(531):  at java.lang.reflect.Method.invoke(Method.java:511)
02-09 13:06:22.170: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-09 13:06:22.170: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-09 13:06:22.170: E/AndroidRuntime(531):  at dalvik.system.NativeStart.main(Native Method)
1
  • the album argument can be null... check it Commented Feb 9, 2012 at 13:55

2 Answers 2

10

you forget to call db.open()

try this

public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == btAdd) {
        Album_db db = new Album_db(this);
        db.open();
        String artiste = ((EditText) this.findViewById(R.id.editArtist))
                .getText().toString();
        String titre = ((EditText) this.findViewById(R.id.editTitle))
                .getText().toString();

        Album album = new Album(artiste, titre);
        db.insertAlbum(album);
        updateList();
        db.close();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

you need to call db.open() method before calling db.insertAlbum method. And afterwards dont forget to call db.close()

and dont forget to close your cursor in getAlbums() before returning value:

cursorResults.close();

3 Comments

thank you for your advice, but what i try to do doesn't really work, I want to add data, and read them into a listview.. i'm getting another nullpointerexception on the updatelist method, listeAlbum.setAdapter(adapter);
you need to call db.open before calling update method and db.close after its done
db.open is not available in SQLiteDatabase objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.