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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:26:23+00:00 2026-06-16T15:26:23+00:00

I have a database file in assets folder.When i try to open this file

  • 0

I have a database file in assets folder.When i try to open this file from DBHelper class it throws "java.lang.NullPointerException" at copyDataBase() method following line,

myInput = myContext.getAssets().open(DB_NAME,AssetManager.ACCESS_STREAMING);

In MyActivity:

private AssetsDBHelper db;
@Override
    protected void onStart() {
        super.onStart();
        db = new AssetsDBHelper(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(db!=null)
        {
            db.close();
        }
    }

AssetsDBHelper class:

public class AssetsDBHelper extends SQLiteOpenHelper {

    public static final String DB_PATH ="/data/data/com.innodea.money/databases/";
    public static final String DB_NAME ="stocksdb";
    public static final int DB_VERSION = 1;
    public static final String TABLE_STOCKS = "stocks";
    public static final String COL_ID="_id";
    public static final String COL_SYMBOL="symbol";
    public static final String COL_NAME="name";
    public static final String COL_EXCHANGE="exchange";

    private Context myContext;
    private SQLiteDatabase myDataBase;

    public AssetsDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        Log.i("AssetsDBHelper","AssetsDBHelper constructor");

        Log.i("AssetsDBHelper","before createDataBase");
        try {
            createDataBase();
            Log.i("AssetsDBHelper","after createDataBase");
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.myContext = context;
    }

    public void createDataBase() throws IOException {
        Log.i("AssetsDBHelper","inside createDataBase");
        boolean dbExist = checkDataBase();

        Log.i("AssetsDBHelper","dbExist->"+dbExist);
        if (!dbExist) {
            Log.i("AssetsDBHelper","dbExist not exist.So get database from assets");
            this.getReadableDatabase();
            // close up the database after we have created it.
            this.close();

            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }

        }
    }

    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
            // database does't exist yet.
        }

        if (checkDB != null) {
            checkDB.close();
        }
        //Return true if DB exists,else return false
        return checkDB != null ? true : false;
    }


    private void copyDataBase() throws IOException {
        Log.i("AssetsDBHelper","inside copyDatabase");

        // Open your local db as the input stream
        InputStream myInput=null;
        // Open the empty db as the output stream
        OutputStream myOutput=null;

        try {
            Log.i("AssetsDBHelper","1");
            myInput = myContext.getAssets().open(DB_NAME,AssetManager.ACCESS_STREAMING);
            Log.i("AssetsDBHelper","2");

            File dir = new File(DB_PATH);
            dir.mkdirs();
            Log.i("AssetsDBHelper","3");

            // Path to the just created empty db
            String outFileName = DB_PATH + DB_NAME;
            Log.i("AssetsDBHelper",outFileName);
            myOutput = new FileOutputStream(outFileName);
            Log.i("AssetsDBHelper","4");
            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            Log.i("AssetsDBHelper","5");
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                Log.i("AssetsDBHelper","6");
                myOutput.write(buffer, 0, length);
            }
            Log.i("AssetsDBHelper","write to "+outFileName);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("AssetsDBHelper","copyDatabase error->"+e.toString());
        }
        finally
        {
            // Close the streams
            if(myOutput!=null)
            {
                myOutput.flush();
                myOutput.close();
            }

            if(myInput!=null)
            {
                myInput.close();
            }
        }
    }

    public SQLiteDatabase openDataBase() throws SQLException {
        // Open the database
        String myPath = DB_PATH + DB_NAME;
        try {
            myDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
            Log.i("AssetsDBHelper", "Database Opened");
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("AssetsDBHelper", "Database not Opened.Error->"+e.toString());
        }
        return myDataBase;
    }

    @Override
    public synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

    public Cursor getWordMatches(String query, String[] columns) {
        String selection = COL_SYMBOL + " MATCH ?";
        String[] selectionArgs = new String[] {query+"*"};
        return query(selection, selectionArgs, columns);
    }

    private Cursor query(String selection, String[] selectionArgs, String[] columns) {
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(TABLE_STOCKS);

        Cursor cursor=null;
        try {
            cursor = builder.query(openDataBase(),columns, selection, selectionArgs, null, null, null);

            if (cursor == null) {
                return null;
            } else if (!cursor.moveToFirst()) {
                cursor.close();
                return null;
            }
        } 
        catch (SQLException e) {
            e.printStackTrace();
        }
        return cursor;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}
  • 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-16T15:26:24+00:00Added an answer on June 16, 2026 at 3:26 pm

    myContext has not yet been assigned when the statement where you get the exception is executed. So shift the assignment up like this:

    public AssetsDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
        // ...etc...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database file .mdf from MS SQL EXPRESS in folder: C:\Program Files\Microsoft
guys, I have the problem when copying database from local assets folder to /data/data/package_name/databases
I have an sqlite file in the assets folder. I'm using a database manager
I have an SQLite database file exported from Scraperwiki with .sqlite file extension. How
I have a database named crea with a table named assets and in this
So I have this class: http://pastebin.com/EwXFwuZz And this directory tree: http://s14.directupload.net/file/d/3099/uskko5mo_png.htm And I'm working
I have a CSV file in the assets folder with more than 10000 lines
I have a database in SQLite precreated and inserted in assets folder, unfortunately I
I have a database named crea with a table named assets and in this
I have created a SQLite file and putted it on to assets folder. Now

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.