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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:33:32+00:00 2026-05-28T18:33:32+00:00

Code for the DBHelper class: class DBHelper extends SQLiteOpenHelper { private static final String

  • 0

Code for the DBHelper class:

class DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_PATH = Environment.getDataDirectory()
            + "/data/test.data/databases/";
    private static final String DATABASE_NAME = "test.sqlite3";
    private static final int SCHEMA_VERSION = 1;
    public static final String TABLE_NAME = "terran_builds";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_OVERVIEW = "overview";

    public SQLiteDatabase dbSqlite;

    private final Context myContext;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
        this.myContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

    public void createDatabase() {
        createDB();
    }

    private void createDB() {

        boolean dbExist = DBExists();
        if (!dbExist) {

            this.getReadableDatabase();

            copyDBFromResource();

        }

    }

    private boolean DBExists() {
        SQLiteDatabase db = null;

        try {
            String databasePath = DATABASE_PATH + DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(databasePath, null,
                    SQLiteDatabase.OPEN_READWRITE);
            db.setLocale(Locale.getDefault());
            db.setLockingEnabled(true);
            db.setVersion(1);
        } catch (SQLiteException e) {
            Log.e("SqlHelper", "database not found");
        }

        if (db != null) {

            db.close();
        }

        return db != null ? true : false;
    }

    private void copyDBFromResource() {

        InputStream inputStream = null;
        OutputStream outStream = null;
        String dbFilePath = DATABASE_PATH + DATABASE_NAME;

        try {

            inputStream = myContext.getAssets().open(DATABASE_NAME);

            outStream = new FileOutputStream(dbFilePath);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, length);
            }
            outStream.flush();
            outStream.close();
            inputStream.close();
        } catch (IOException e) {

            throw new Error("Problem copying database from resource file.");
        }

    }

    public void openDataBase() throws SQLException {

        String myPath = DATABASE_PATH + DATABASE_NAME;
        dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public synchronized void close() {
        // TODO Auto-generated method stub

        if (dbSqlite != null) {
            dbSqlite.close();
        }
        super.close();
    }

    public Cursor getTerran() {

        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        queryBuilder.setTables(TABLE_NAME);

        String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_NAME,
                COLUMN_OVERVIEW };

        Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                null, null, null, "name");

        return mCursor;
    }

    public String getName(Cursor c) {
        return (c.getString(1));
    }
}

Code for the Terran activity:

public class Terran extends Activity {

    private DBHelper dbHelp = null;
    private Cursor ourCursor = null;
    private DBAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        try {

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

            // this is our listview element, obtained by id from our xml layout
            ListView myListView = (ListView) findViewById(R.id.tvterran);

            // create our database helper
            dbHelp = new DBHelper(this);
            // we call the create right afther initializing helper
            dbHelp.createDatabase();
            // open the database
            dbHelp.openDataBase();
            // get our cursor
            ourCursor = dbHelp.getTerran();

            // tell android to start managing cursor
            startManagingCursor(ourCursor);
            // create our adapter
            adapter = new DBAdapter(ourCursor);
            // set the adapter
            myListView.setAdapter(adapter);

        } catch (Exception e) {

            // send real error message
            Log.e("ERROR", "ERROR IN CODE: " + e.toString());

            e.printStackTrace();
        }
    }

    class DBAdapter extends CursorAdapter {
        DBAdapter(Cursor c) {
            super(Terran.this, c);
        }

        @Override
        public void bindView(View row, Context ctxt, Cursor c) {
            // TODO Auto-generated method stub
            terranHolder holder = (terranHolder) row.getTag();
            holder.populateFrom(c, dbHelp);

        }

        @Override
        public View newView(Context ctxt, Cursor c, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.terran_layout, parent, false);
            terranHolder holder = new terranHolder(row);
            row.setTag(holder);
            return (row);
        }
    }

    static class terranHolder {
        private TextView name = null;

        terranHolder(View row) {
            name = (TextView) row.findViewById(R.id.tvterran);
        }

        void populateFrom(Cursor c, DBHelper r) {
            name.setText(r.getName(c));
        }
    }

}

Logcat exception:

01-30 20:07:13.765: E/ERROR(7153): ERROR IN CODE: java.lang.NullPointerException
01-30 20:07:13.775: W/System.err(7153): java.lang.NullPointerException
01-30 20:07:13.775: W/System.err(7153):     at com.subdeveloper.starcraftbuilds.Terran.onCreate(Terran.java:46)
01-30 20:07:13.775: W/System.err(7153):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-30 20:07:13.775: W/System.err(7153):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
01-30 20:07:13.775: W/System.err(7153):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1499)
01-30 20:07:13.775: W/System.err(7153):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
01-30 20:07:13.775: W/System.err(7153):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
01-30 20:07:13.775: W/System.err(7153):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:731)
01-30 20:07:13.775: W/System.err(7153):     at android.widget.TabHost.setCurrentTab(TabHost.java:403)
01-30 20:07:13.775: W/System.err(7153):     at android.widget.TabHost.addTab(TabHost.java:242)

I can’t get the ListView to generate from the query results. From my debugging I understand that the error is in the Terran class on line 46. I can’t figure out what is wrong. Tested with other database, database connection works.

  • 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-28T18:33:34+00:00Added an answer on May 28, 2026 at 6:33 pm

    Your ListView is null that means that either don’t have a ListView with that id in your layout or android didn’t generate the proper id for the list and you have an old one that is incorrect.

    Try to refresh the project, delete the R class from the folder gen so that the ID are generated again, restart the IDE.

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

Sidebar

Related Questions

I have the following code snippet. public class ImageStoreActivity extends ListActivity { private DBHelper
Okay full code now: DBOpenHelper: public class DBOpenHelper extends SQLiteOpenHelper { private SQLiteDatabase mDatabase;
friends, i am using following code inside asyncTask public class AsycLoaderFromDbAndMapInjector extends AsyncTask {
Here is my code for my db class: package one.two; import java.util.List; import android.app.ListActivity;
Following code is used to insert data in the database. When application is launched,
I have created a sub class (DatabaseHelper) of SQLiteOpenHelper Class.Its has a constructor and
I have this code: if (idListe.size()>0) { strIdArray = new String[idListe.size()]; idListe.toArray(strIdArray); //my query
I made a class for DB connection like this: public class DbHelper : IDisposable
Code goes first: class A { public: ... int *foo() const { return _px;
Code: public interface IFoo { void Bar(); } public class FooClass : IFoo {

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.