Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:49:01+00:00 2026-05-29T09:49:01+00:00

i’m a new android developper and i’m getting a null pointer exception when i

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:49:02+00:00Added an answer on May 29, 2026 at 9:49 am

    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();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I have a reasonable size flat file database of text documents mostly saved in
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.