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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:42:06+00:00 2026-06-05T17:42:06+00:00

I am writing code for a database to fetch data in an activity which

  • 0

I am writing code for a database to fetch data in an activity which contains three fields,
namely Truck_id, employee_id, and password.

I am getting some errors; below is my code:
Please help me as I am new to android:

public class DataBaseHelper extends SQLiteOpenHelper {

// default system path of your application database.
// private static String DB_PATH = "/data/data/com.tanzanite.operasoft/databases/";

private static String DB_PATH = "/sdcard/";

private static String DB_NAME = "snowman";

private static SQLiteDatabase myDataBase;

private Context myContext;

private DataBaseHelper dbHelper;


/**
 * Constructor Takes and keeps a reference of the passed context in order to
 * access to the application assets and resources.
 * 
 * @param context
 */
public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);

    this.myContext = context;
}

public void TestAdapter(Context context)
{
    this.myContext= context;
}

public static final String TABLE_NAME = "Login";
private static final int DATABASE_VERSION= 2;

private static final String DATABASE_NAME="snowman";


public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "employee_id";  
public static final String COLUMN_TRUCKID = "truck_id";


public static final int NAME_COLUMN=1;
public static final String KEY_CREATION_DATE="creation_date";
public static final String KEY_TASK="task";


/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */

private static final String DATABASE_CREATE = "create table "
        + TABLE_NAME + "( " + COLUMN_ID + " integer primary key autoincrement, " 
                            + COLUMN_TRUCKID+ " text not null, "
                            + COLUMN_NAME+ " text not null, )";


public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        // do nothing - database already exist
    } else {

        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gonna be able to overwrite that
        // database with our database.
        //this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

public DataBaseHelper open() throws SQLException
{   
        //myDataBase = dbHelper.getWritableDatabase();
        return this;
}


/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 * 
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {

SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath ); 
         SQLiteDatabase.openOrCreateDatabase(dbfile, null);

        //throw new Error("Database does't exist yet.");

    }

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[128];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException {

    // Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    return;

}

public String logindetails()
{
    open();
    Cursor c=myDataBase.query(TABLE_NAME,
             new String[]{COLUMN_NAME,
                          COLUMN_TRUCKID,

                          },null, null, null, null, null);
    String b="";

    c.close();
    myDataBase.close();
    return b;
}   

public void insert(String a, String b){

    ContentValues vals = new ContentValues();
    vals.put(COLUMN_TRUCKID, "CM-3456");
    vals.put(COLUMN_NAME,"Sachin");
    //vals.put(KEY_PIN, "12345");

    vals.put(COLUMN_TRUCKID, "HR-6788");
    vals.put(COLUMN_NAME,"Sameer");
    //vals.put(KEY_PIN, "54321");

    vals.put(COLUMN_ID, "AM-6123");
    vals.put(COLUMN_NAME,"Sahu");
    //vals.put(KEY_PIN, "3452");


    myDataBase.close();


}

public Cursor fetchdata()
{
    open();
    Cursor c=myDataBase.query(TABLE_NAME,
         new String[]{COLUMN_NAME,
            COLUMN_TRUCKID,

                      },null, null, null, null, null);
    c.moveToLast();
    myDataBase.close();
    return c;
}


public static SQLiteDatabase getDataBase() {
    return myDataBase;

}

@Override
public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {
    try{

    db.execSQL(DATABASE_CREATE);
    }catch(Exception e)
    {
        Log.v( "Press_Data","exception in table created");
    }

}

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

}

The errors are..

06-14 18:22:27.233: D/dalvikvm(25087): GC_CONCURRENT freed 757K, 48% free 3547K/6727K, external 1625K/2137K, paused 4ms+4ms
06-14 18:22:27.393: I/net.osmand(25087): BaseOsmandRender Init render default for 738 ms
06-14 18:22:27.523: D/dalvikvm(25087): GC_EXTERNAL_ALLOC freed 123K, 46% free 3635K/6727K, external 1625K/2137K, paused 62ms
06-14 18:22:27.633: D/AndroidRuntime(25087): Shutting down VM
06-14 18:22:27.633: W/dalvikvm(25087): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 18:22:27.663: E/AndroidRuntime(25087): FATAL EXCEPTION: main
06-14 18:22:27.663: E/AndroidRuntime(25087): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tanzanite.operasoft/com.tanzanite.operasoft.activity.Sw_LoginScreenActivity}: java.lang.NullPointerException
06-14 18:22:27.663: E/AndroidRuntime(25087):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at android.os.Looper.loop(Looper.java:130)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at java.lang.reflect.Method.invoke(Method.java:507)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at dalvik.system.NativeStart.main(Native Method)
06-14 18:22:27.663: E/AndroidRuntime(25087): Caused by: java.lang.NullPointerException
06-14 18:22:27.663: E/AndroidRuntime(25087):    at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:222)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:52)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 18:22:27.663: E/AndroidRuntime(25087):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 18:22:27.663: E/AndroidRuntime(25087):    ... 11 more
06-14 18:28:37.603: D/dalvikvm(26440): GC_CONCURRENT freed 757K, 48% free 3541K/6727K, external 1625K/2137K, paused 3ms+3ms
06-14 18:28:37.773: I/net.osmand(26440): BaseOsmandRender Init render default for 611 ms
06-14 18:28:37.873: D/dalvikvm(26440): GC_EXTERNAL_ALLOC freed 122K, 46% free 3633K/6727K, external 1625K/2137K, paused 51ms
06-14 18:28:38.013: D/AndroidRuntime(26440): Shutting down VM
06-14 18:28:38.013: W/dalvikvm(26440): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 18:28:38.033: E/AndroidRuntime(26440): FATAL EXCEPTION: main
06-14 18:28:38.033: E/AndroidRuntime(26440): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tanzanite.operasoft/com.tanzanite.operasoft.activity.Sw_LoginScreenActivity}: java.lang.NullPointerException
06-14 18:28:38.033: E/AndroidRuntime(26440):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at android.os.Looper.loop(Looper.java:130)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at java.lang.reflect.Method.invoke(Method.java:507)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at dalvik.system.NativeStart.main(Native Method)
06-14 18:28:38.033: E/AndroidRuntime(26440): Caused by: java.lang.NullPointerException
06-14 18:28:38.033: E/AndroidRuntime(26440):    at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:222)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:52)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 18:28:38.033: E/AndroidRuntime(26440):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 18:28:38.033: E/AndroidRuntime(26440):    ... 11 more
06-14 18:40:55.423: D/dalvikvm(29844): GC_CONCURRENT freed 759K, 48% free 3553K/6727K, external 1625K/2137K, paused 5ms+3ms
06-14 18:40:55.633: I/net.osmand(29844): BaseOsmandRender Init render default for 841 ms
06-14 18:40:55.773: D/dalvikvm(29844): GC_EXTERNAL_ALLOC freed 122K, 46% free 3633K/6727K, external 1625K/2137K, paused 58ms
06-14 18:40:55.913: D/AndroidRuntime(29844): Shutting down VM
06-14 18:40:55.913: W/dalvikvm(29844): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 18:40:55.943: E/AndroidRuntime(29844): FATAL EXCEPTION: main
06-14 18:40:55.943: E/AndroidRuntime(29844): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tanzanite.operasoft/com.tanzanite.operasoft.activity.Sw_LoginScreenActivity}: java.lang.NullPointerException
06-14 18:40:55.943: E/AndroidRuntime(29844):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at android.os.Looper.loop(Looper.java:130)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at java.lang.reflect.Method.invoke(Method.java:507)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at dalvik.system.NativeStart.main(Native Method)
06-14 18:40:55.943: E/AndroidRuntime(29844): Caused by: java.lang.NullPointerException
06-14 18:40:55.943: E/AndroidRuntime(29844):    at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:223)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:52)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 18:40:55.943: E/AndroidRuntime(29844):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 18:40:55.943: E/AndroidRuntime(29844):    ... 11 more
06-14 19:12:06.293: D/dalvikvm(5930): GC_CONCURRENT freed 763K, 48% free 3549K/6727K, external 1625K/2137K, paused 4ms+5ms
06-14 19:12:06.443: I/net.osmand(5930): BaseOsmandRender Init render default for 689 ms
06-14 19:12:06.553: D/dalvikvm(5930): GC_EXTERNAL_ALLOC freed 120K, 47% free 3632K/6727K, external 1625K/2137K, paused 64ms
06-14 19:12:06.703: D/AndroidRuntime(5930): Shutting down VM
06-14 19:12:06.703: W/dalvikvm(5930): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 19:12:06.723: E/AndroidRuntime(5930): FATAL EXCEPTION: main
06-14 19:12:06.723: E/AndroidRuntime(5930): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tanzanite.operasoft/com.tanzanite.operasoft.activity.Sw_LoginScreenActivity}: java.lang.NullPointerException
06-14 19:12:06.723: E/AndroidRuntime(5930):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at android.os.Looper.loop(Looper.java:130)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at java.lang.reflect.Method.invoke(Method.java:507)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at dalvik.system.NativeStart.main(Native Method)
06-14 19:12:06.723: E/AndroidRuntime(5930): Caused by: java.lang.NullPointerException
06-14 19:12:06.723: E/AndroidRuntime(5930):     at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:224)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:49)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 19:12:06.723: E/AndroidRuntime(5930):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 19:12:06.723: E/AndroidRuntime(5930):     ... 11 more
  • 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-05T17:42:07+00:00Added an answer on June 5, 2026 at 5:42 pm

    From you log i see

    Caused by: java.lang.NullPointerException 
    06-14 19:12:06.723: E/AndroidRuntime(5930): at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:224) 
    06-14 19:12:06.723: E/AndroidRuntime(5930): at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:49)
    

    So, you need use debuger and set breackpoints on

    Sw_LoginScreenActivity.java – on line 49 (i think there you try to fetch data)

    DataBaseHelper.java – on 224 – there you try to fetch data from myDataBase

    public Cursor fetchdata()
    {
        open();
        Cursor c=myDataBase.query(TABLE_NAME,
             new String[]{COLUMN_NAME,
                COLUMN_TRUCKID,
    
                          },null, null, null, null, null);
        c.moveToLast();
        myDataBase.close();
        return c;
    }
    

    But, in open() method

    public DataBaseHelper open() throws SQLException
    {   
            //myDataBase = dbHelper.getWritableDatabase();
            return this;
    }
    

    You comment line with creating database instance – so next line “Cursor c=myDataBase.query(TABLE_NAME,” it’s not correct, because myDataBase is null.

    You did not open connection to database.

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

Sidebar

Related Questions

I am writing code to migrate data from our live Access database to a
I've got a situation which I want to fetch data from a database, and
I am writing code that pulls data from database tables and writes them to
I'm writing a simple code generation application to build POCO's from a DB2 database
I'm writing code that uses sched_setaffinity, which requires kernel 2.5.8 or later. I've been
I've been writing code that processes certain fields of an object by modifying their
I'm am writing code which calculates an insurance premium up to the 65th birthday.
I am writing code in c++. I need to support few basic data types
I am writing my multilevel navigation element(s), data coming from database. When I click
I am writing some code in C which connects to MYSQL server. I am

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.