0
votes

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
Where's the rest of the logcat? There should be more to tell you where the NPE is occurringcodeMagic
post more of the stacktracenjzk2
Extra tip : reading full stacktraces helps. Here, it tells you that the error comes from the fact that something is null in DataListView.openAndQueryDatabase line 45.njzk2

1 Answers

2
votes
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.