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

  • Home
  • SEARCH
  • 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 9117735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:57:59+00:00 2026-06-17T04:57:59+00:00

I am trying to create a db, insert into the db, pull from the

  • 0

I am trying to create a db, insert into the db, pull from the db and display the contents (long) to a TextView.

I think I am creating and inserting into the db correctly. In my getScore() method I am getting a NullPointerException.

Any help on code structure is much appreciated as well! Thank you in advance.

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper { 

    SQLiteDatabase db;

    // Table columns names. 
    private static final String SCORE = "score"; 

    public DatabaseHelper(Context context) { 
        super(context, DB_NAME, null, DATABASE_VERSION); 
    }

    public SQLiteDatabase openDB() {
        db = this.getWritableDatabase();
        return db;
    }

    public long getScore(SQLiteDatabase db) {
        //Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + " WHERE " + SCORE + " = " + 11 + ";", null);  //Line 45
        Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + ";", null);  //Line 39
        long i = 0;
        if(c.getCount() == 0) {
            i = 333;
        } else if (c.getCount() == 1) {
            i = 444;
        } else {
            i = 888;
               /*c.moveToFirst();
               int columnIndex = c.getInt(c.getColumnIndex("SCORE"));
               if(columnIndex != -1) {
                   i = c.getLong(columnIndex);
               } else { 
                   i = 999; 
               }*/
        }
        c.close();
        return i;
    }

    //Insert new record.
    public long insert(long score, int percentage) {
        ContentValues values = new ContentValues();
        values.put(SCORE, score);
        values.put(PERCENTAGE, percentage);

        return db.insert(TABLE, null, values);
    }

    public synchronized void closeDB(SQLiteDatabase db) {
        if(db != null) {
            db.close();
        }
        super.close();
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE + " ("
                + RANK + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + SCORE + " LONG,"
                + PERCENTAGE + " INTEGER"
                + ");");
    }
}

Highscores.java

public class Highscores extends Activity {

    DatabaseHelper dh;
    SQLiteDatabase db;
    long scores;

    TextView score;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscoresmain);

        dh = new DatabaseHelper(this);
        dh.openDB();
        long x = 11;
        int y = 22;

        TextView score = (TextView)findViewById(R.id.score);        
        TextView r1s = (TextView)findViewById(R.id.r1s);

        dh.insert(x, y);
        scores = dh.getScore(db);  //Line 56            
        score.setText("Score Column - TEST");
        r1s.setText("" + score);

        dh.closeDB(db);
    }
}

LogCat output

01-03 17:36:01.172: E/AndroidRuntime(1941): FATAL EXCEPTION: main
01-03 17:36:01.172: E/AndroidRuntime(1941): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Highscores}: java.lang.NullPointerException
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.os.Looper.loop(Looper.java:137)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at dalvik.system.NativeStart.main(Native Method)
01-03 17:36:01.172: E/AndroidRuntime(1941): Caused by: java.lang.NullPointerException
01-03 17:36:01.172: E/AndroidRuntime(1941):     at com.example.test.DatabaseHelper.getScore(DatabaseHelper.java:39)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at com.example.test.Highscores.onCreate(Highscores.java:56)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.app.Activity.performCreate(Activity.java:5104)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-03 17:36:01.172: E/AndroidRuntime(1941):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-03 17:36:01.172: E/AndroidRuntime(1941):     ... 11 more

EDIT: New getScore() method.

public long getScore() {
    Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + " WHERE " + SCORE + " = " + 11 + ";", null);
    //Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + ";", null);
    long i = 0;
    if(c.getCount() == 0) {
        i = 333;
    } else if (c.getCount() == 1) {
        i = 444;
    } else if (c.getCount() == 2) {
        i = 555;
    } else {
        i = 666;
           /*c.moveToFirst();
           int columnIndex = c.getInt(c.getColumnIndex("SCORE"));
           if(columnIndex != -1) {
               i = c.getLong(columnIndex);
           } else { 
               i = 999; 
           }*/
    }
    c.close();
    return i;
}

This still returns i with a value of 666.

  • 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-06-17T04:58:01+00:00Added an answer on June 17, 2026 at 4:58 am

    You’re overriding your local db value here: public long getScore(SQLiteDatabase db) { ... . Remove that parameter and it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a stored procedure to insert data into 2 tables
I'm trying to create a function to insert data into a table. The query
I'm trying to create sql statement in TSQL that looks like this: INSERT INTO
I am trying to create a simplified code to insert images dynamically into a
I am trying to create a global price markup that will insert into the
Iam trying to create a string for using in execute_immediate statement for inserting into
I am trying to create a new row to a database using insert into
I am trying to create a simple insert trigger from Products table to ProductPrice
I'm trying to assign integer to a newly created row: INSERT INTO wcategory (name,
I'm trying to create a post button that inserts the latest posts into a

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.