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

The Archive Base Latest Questions

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

Class #1 – Has to get data from DB and prepare ListAdapter: public class

  • 0

Class #1 – Has to get data from DB and prepare ListAdapter:

public class DataListView extends ListActivity {

    public ArrayList<String> results = new ArrayList<String>();
    public String tableName = DBHelper.tableName;
    public SQLiteDatabase newDB;

    public void displayResultList() {
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));
        getListView().setTextFilterEnabled(true);
    }

    public void openAndQueryDatabase() {
        try {
            DBHelper dbHelper = new DBHelper(this.getApplicationContext());
            newDB = dbHelper.getWritableDatabase();
            Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM " +
                    tableName, null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String firstName = c.getString(c.getColumnIndex("FirstName"));
                        int age = c.getInt(c.getColumnIndex("Age"));
                        results.add("Name: " + firstName + ", Age: " + age);
                    }while (c.moveToNext());
                } 
            }           
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (newDB != null) 
                newDB.execSQL("DELETE FROM " + tableName);
                newDB.close();
        }

    }
}

Class #2 – Has to show the List:

public class StartActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         DataListView list = new DataListView();
         list.openAndQueryDatabase();
         list.displayResultList();
    }
}

But it throws an error:

01-02 18:22:25.584: E/AndroidRuntime(30425): FATAL EXCEPTION: main
01-02 18:22:25.584: E/AndroidRuntime(30425):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.saigmn/com.saigmn.StartActivity}:
java.lang.NullPointerException 01-02 18:22:25.584:
E/AndroidRuntime(30425): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-02 18:22:25.584: E/AndroidRuntime(30425): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-02 18:22:25.584: E/AndroidRuntime(30425): at
android.app.ActivityThread.access$600(ActivityThread.java:123) 01-02
18:22:25.584: E/AndroidRuntime(30425): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-02 18:22:25.584: E/AndroidRuntime(30425): at
android.os.Handler.dispatchMessage(Handler.java:99) 01-02
18:22:25.584: E/AndroidRuntime(30425): at
android.os.Looper.loop(Looper.java:137) 01-02 18:22:25.584:
E/AndroidRuntime(30425): at
android.app.ActivityThread.main(ActivityThread.java:4424) 01-02
18:22:25.584: E/AndroidRuntime(30425): at
java.lang.reflect.Method.invokeNative(Native Method) 01-02
18:22:25.584: E/AndroidRuntime(30425): at
java.lang.reflect.Method.invoke(Method.java:511) 01-02 18:22:25.584:
E/AndroidRuntime(30425): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-02 18:22:25.584: E/AndroidRuntime(30425): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-02
18:22:25.584: E/AndroidRuntime(30425): at
dalvik.system.NativeStart.main(Native Method) 01-02 18:22:25.584:
E/AndroidRuntime(30425): Caused by: java.lang.NullPointerException
01-02 18:22:25.584: E/AndroidRuntime(30425): at
com.saigmn.DataListView.openAndQueryDatabase(DataListView.java:45)
01-02 18:22:25.584: E/AndroidRuntime(30425): at
com.saigmn.StartActivity.onCreate(StartActivity.java:20) 01-02
18:22:25.584: E/AndroidRuntime(30425): at
android.app.Activity.performCreate(Activity.java:4492) 01-02
18:22:25.584: E/AndroidRuntime(30425): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-02 18:22:25.584: E/AndroidRuntime(30425): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

After some testing I discovered that there is smth inside openAndQueryDatabase().

Also when putting both called methods into StartActivity class – it works fine.

Please help me to find out this issue.

  • 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:48:19+00:00Added an answer on June 16, 2026 at 3:48 pm
    if (newDB != null) 
      newDB.execSQL("DELETE FROM " + tableName);
      newDB.close();
    

    This could probably be the source. Put both statements within the if block to ensure a close() call isn’t made on a null newDB instance. Try:

    if (newDB != null) 
    { 
      newDB.execSQL("DELETE FROM " + tableName);
      newDB.close();
    }
    

    Now I’m pretty sure this is the source of your NullPointer. Check line 45 in DataListView.java.

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

Sidebar

Related Questions

class TreeNode { public string Value { get; set;} public Collection<TreeNode> Nodes { get;
class Cat{ } class Tiger extends Cat{ public String getZooAddress(){ return cityZoo; }; }
class Test { public static void main(String[] args) throws Exception { Test t =
class a{ public void foo(int a){ System.out.println(super); } } class b extends a{ public
class EventDataValue { public: enum Types { NONE, INT, STRING, DOUBLE, ULONG }; EventDataValue()
class A { public static void main(String[] args) { A a = new A();
class A { String s4 = abc; static public void main(String[]args ) { String
class Monitor { public: Monitor(string someData); Person person_; } class Person { public: Person(string
class Item { private int address; private String itemString; public Item(String item) { separate(item);
class TestMe{ public static void main (String args[]){ String s3; System.out.print(s3); } } why

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.