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 8752851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:18:47+00:00 2026-06-13T13:18:47+00:00

I want to pass a user ID from the SQLite Database but I’m having

  • 0

I want to pass a user ID from the SQLite Database but I’m having hard time figure it out.
I’ve been doing my research but because of my level of java skill is low, I couldn’t make any further move. Please help me to resolve this and It would be really appreciated it.

This is the errorLog

10-30 23:04:13.795: E/AndroidRuntime(22860): FATAL EXCEPTION: main
10-30 23:04:13.795: E/AndroidRuntime(22860): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.android/com.app.android.MainActivity}: java.lang.NullPointerException
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.os.Looper.loop(Looper.java:137)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.main(ActivityThread.java:4517)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at java.lang.reflect.Method.invokeNative(Native Method)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at java.lang.reflect.Method.invoke(Method.java:511)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at dalvik.system.NativeStart.main(Native Method)
10-30 23:04:13.795: E/AndroidRuntime(22860): Caused by: java.lang.NullPointerException
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at com.app.android.MainActivity.<init>(MainActivity.java:16)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at java.lang.Class.newInstanceImpl(Native Method)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at java.lang.Class.newInstance(Class.java:1319)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
10-30 23:04:13.795: E/AndroidRuntime(22860):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)

DatabaseHandler Class

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_NAME = "app";
    private static final String TABLE_LOGIN = "login";

    private static final String KEY_ID = "id";  
    private static final String KEY_NAME = "name";  

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"          
                + KEY_NAME + " TEXT" + ")";
        db.execSQL(CREATE_LOGIN_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
        onCreate(db);
    }

    public void addUser(String id, String name) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID, id); // id       
        values.put(KEY_NAME, name); // name     
        db.insert(TABLE_LOGIN, null, values);
        db.close(); 
    }

    public HashMap<String, String> getUserDetails(){
        HashMap<String,String> user = new HashMap<String,String>();
        String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        cursor.moveToFirst();
        if(cursor.getCount() > 0){
            user.put("id", cursor.getString(1));            
            user.put("name", cursor.getString(2));          

        }
        cursor.close();
        db.close();

        return user;
    }

    public int getUserCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int rowCount = cursor.getCount();
        db.close();
        cursor.close();

        return rowCount;
    }

    public Cursor getUserID() {
        String qry = "SELECT id FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();     
        Cursor cursor = db.rawQuery(qry, null);
        return cursor;

    }

}

MainActivity Class

public class MainActivity extends Activity {

    DatabaseHandler db = new DatabaseHandler(getApplicationContext());

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

        Cursor id = db.getUserID();

        if(id.equals("0065")){

        Intent tab1 = new Intent(getApplicationContext(), RequestActivity.class);                                           
        tab1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(tab1);    

        finish();

        }else{

        Intent tab = new Intent(getApplicationContext(), ReceiveActivity.class);                
        tab.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(tab);

        finish();
    }

    }

}
  • 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-13T13:18:48+00:00Added an answer on June 13, 2026 at 1:18 pm

    I don’t think the error is really there. You have a NullPointer somewhere.
    The error in the logcat is caused by this:

    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    

    You should initialize that db variable in onCreate()

    Anyway, the right way for you to return the ID should be:

    public int getUserID() {
        String qry = "SELECT id FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();     
        Cursor cursor = db.rawQuery(qry, null);
        int res = id.getInt(id.getColumnIndex("id"));
        cursor.close();
        return res;
    }
    

    (don’t forget to close the cursor

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

Sidebar

Related Questions

i have this function pymssql.connect(host=my host,user=my user,password=my pass,database=mydb) I want to read the user
I need to pass user_id, not logged in user, but user_id from view. I
I want to pass values(inputs from user)from view controller class to objective c class
My data is just number, I want pass it from VC1 to VC2 for
I want to pass parameters from PHP Command Line Interface, and then read in
I want to create a postgres user that can access only one database on
I have a uitableview that is populated from a sqlite query. I want to
I know how to pass data from one activity to another. But what I
I have a number of database tables below. I want to display each user
How can I pass a user ID from my login page to an end

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.