-2

im developing a map application in android that search for places in the database based on the user's search input. im trying to query the sqlite database using a field which will search in the database and return the latitude of the places which all in the database.The field is not a primary key below are the codes. Please i need Help. the database class to create and access database.

public class Places {
private Context context;
private DbHelper dataHelper;
private SQLiteDatabase database;

//columns for places table
public static final String ROW_ID="_id";
public static final String P_NAME="placename";
public static final String G_NAME="genericname";
public static final String LONGITUDE="longitude";
public static final String LATITUDE="latitude";
public static final String CONTACT="contactnumbers";



private static final String DB_NAME="MAP.db";
private static final String DB_TABLE_PLACES="places";
private static final String DB_TABLE_EXAMS="exam";
private static final String DB_TABLE_SMS="message";
private static final int DB_VERSION= 1;

//constructor for Places.java

 public Places(Context c){
     this.context = c;
 }

//database helper class

private static class DbHelper extends SQLiteOpenHelper  {


 //constructor for DbHelper inner class

public DbHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    //creating table PLACES
    db.execSQL("CREATE TABLE "+DB_TABLE_PLACES+"("+ROW_ID+" PRIMARY KEY, " +
            ""+P_NAME+" TEXT NOT NULL, " +
            ""+G_NAME+" TEXT NOT NULL, " +
            ""+LONGITUDE+" TEXT NOT NULL, " +
            ""+LATITUDE+" TEXT NOT NULL, " +
            ""+CONTACT+" TEXT );");



    @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE_PLACES);
    db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE_EXAMS);
    db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE_SMS);

    onCreate(db);
}

}

  public Places openDatabase() throws SQLiteException{
   dataHelper = new DbHelper(context);
   database= dataHelper.getWritableDatabase();
return this;

}

  public void closeDatabase(){
dataHelper.close();
database.close();

}

@SuppressWarnings("null")
 public List<String> getPlaceLatitude(String string) {
// TODO Auto-generated method stub

List<String> latitudes = null;
String[] columns= new String[]{ROW_ID,P_NAME,LATITUDE,LONGITUDE,G_NAME,CONTACT};

Cursor c = database.query(DB_TABLE_PLACES, columns,G_NAME+" = "+string, null, null, null, null);

int iLatitude = c.getColumnIndex(LATITUDE);
if (c !=null){
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
    latitudes.add(c.getString(iLatitude));
}
}
return latitudes;}

method that triggers action to query database.

public void searchDbAndOpenIntent(String search){
try{
        Places placeDb = new Places(this);
        placeDb.openDatabase();
        List<String> longitude1= placeDb.getPlaceLongitude(search);
                List<String> latitude1=placeDb.getPlaceLatitude(search);
        placeDb.closeDatabase();
}
                  catch (Exception e) {
            // TODO:handle exception                                    Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
        }
}




public void searchDbAndOpenIntent(String search){
try{
        Places placeDb = new Places(this);
        placeDb.openDatabase();
        List<String> longitude1= placeDb.getPlaceLongitude(search);
                List<String> latitude1=placeDb.getPlaceLatitude(search);
        placeDb.closeDatabase();



        }
                  catch (Exception e) {
            // TODO: handle exception


            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
        }
}

please i want to return the latitude based on the the generic name of the places,but when i try to query the generic_name column it returns java.lang.NullPointerexception .below it the log cat i get when the button that query the database when pressed

       05-14 20:52:37.202: D/ddm-heap(243): Got feature list request
       05-14 20:52:41.123: E/ActivityThread(243): Failed to find provider info for       com.google.settings
       05-14 20:52:41.132: E/ActivityThread(243): Failed to find provider info for   com.google.settings
       05-14 20:52:41.172: E/ActivityThread(243): Failed to find provider info for com.google.settings
       05-14 20:52:41.562: D/dalvikvm(243): GC freed 2007 objects / 134424 bytes in 125ms
       05-14 20:52:41.802: D/LocationManager(243): Constructor: service =   android.location.ILocationManager$Stub$Proxy@44e8c078
       05-14 20:52:42.012: I/MapActivity(243): Handling network change  notification:CONNECTED
       05-14 20:52:42.012: E/MapActivity(243): Couldn't get connection factory client
       05-14 20:52:42.453: D/dalvikvm(243): GC freed 4934 objects / 288736 bytes in 136ms
       05-14 20:52:48.852: D/dalvikvm(243): GC freed 10599 objects / 817800 bytes in  137ms
       05-14 20:53:56.712: W/KeyCharacterMap(243): No keyboard for id 0
       05-14 20:53:56.723: W/KeyCharacterMap(243): Using default keymap: /system /usr/keychars/qwerty.kcm.bin
       05-14 20:53:59.712: D/LocationManager(243): removeUpdates: listener =  my.project.success.KnustP2PActivity@44e93c70
       05-14 20:54:11.812: D/dalvikvm(243): GC freed 5940 objects / 725376 bytes in 98ms
       05-14 20:54:11.812: I/dalvikvm(243): Uncaught exception thrown by finalizer (will be discarded):
       05-14 20:54:11.812: I/dalvikvm(243): Ljava/lang/IllegalStateException;:  Finalizing cursor android.database.sqlite.SQLiteCursor@44f31670 on places that has not  been deactivated or closed
       05-14 20:54:11.812: I/dalvikvm(243):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
       05-14 20:54:11.822: I/dalvikvm(243):     at dalvik.system.NativeStart.run(Native Method)
       05-14 20:54:11.872: E/Database(243): Leak found
       05-14 20:54:11.872: E/Database(243): java.lang.IllegalStateException: /data/data/my.project.success/databases/MAP.db SQLiteDatabase created and never closed
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1694)
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:738)
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:760)
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:753)
       05-14 20:54:11.872: E/Database(243):     at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:473)
       05-14 20:54:11.872: E/Database(243):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
       05-14 20:54:11.872: E/Database(243):     at my.project.success.Places.openDatabase(Places.java:201)
       05-14 20:54:11.872: E/Database(243):     at my.project.success.SearchActivity.searchDbAndOpenIntent(SearchActivity.java:217)
       05-14 20:54:11.872: E/Database(243):     at my.project.success.SearchActivity.onClick(SearchActivity.java:151)
       05-14 20:54:11.872: E/Database(243):     at android.view.View.performClick(View.java:2364)
       05-14 20:54:11.872: E/Database(243):     at android.view.View.onTouchEvent(View.java:4179)
       05-14 20:54:11.872: E/Database(243):     at android.widget.TextView.onTouchEvent(TextView.java:6540)
       05-14 20:54:11.872: E/Database(243):     at android.view.View.dispatchTouchEvent(View.java:3709)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
       05-14 20:54:11.872: E/Database(243):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
       05-14 20:54:11.872: E/Database(243):     at android.os.Handler.dispatchMessage(Handler.java:99)
       05-14 20:54:11.872: E/Database(243):     at android.os.Looper.loop(Looper.java:123)
       05-14 20:54:11.872: E/Database(243):     at     android.app.ActivityThread.main(ActivityThread.java:4363)
       05-14 20:54:11.872: E/Database(243):     at java.lang.reflect.Method.invokeNative(Native Method)
       05-14 20:54:11.872: E/Database(243):     at java.lang.reflect.Method.invoke(Method.java:521)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
       05-14 20:54:11.872: E/Database(243):     at dalvik.system.NativeStart.main(Native Method)
       05-14 20:54:12.014: I/dalvikvm(243): Uncaught exception thrown by finalizer (will be discarded):
       05-14 20:54:12.022: I/dalvikvm(243): Ljava/lang/IllegalStateException;: Finalizing cursor  android.database.sqlite.SQLiteCursor@44f2f0b0 on places that has not been deactivated or closed
       05-14 20:54:12.022: I/dalvikvm(243):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
       05-14 20:54:12.032: I/dalvikvm(243):     at dalvik.system.NativeStart.run(Native Method)
       05-14 20:54:12.152: E/Database(243): Leak found
       05-14 20:54:12.152: E/Database(243): java.lang.IllegalStateException: /data/data/my.project.success/databases/MAP.db SQLiteDatabase created and never closed
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1694)
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:738)
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:760)
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:753)
       05-14 20:54:12.152: E/Database(243):     at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:473)
       05-14 20:54:12.152: E/Database(243):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
       05-14 20:54:12.152: E/Database(243):     at my.project.success.Places.openDatabase(Places.java:201)
       05-14 20:54:12.152: E/Database(243):     at my.project.success.SearchActivity.searchDbAndOpenIntent(SearchActivity.java:217)
       05-14 20:54:12.152: E/Database(243):     at my.project.success.SearchActivity.onClick(SearchActivity.java:79)
       05-14 20:54:12.152: E/Database(243):     at android.view.View.performClick(View.java:2364)
       05-14 20:54:12.152: E/Database(243):     at android.view.View.onTouchEvent(View.java:4179)
       05-14 20:54:12.152: E/Database(243):     at android.widget.TextView.onTouchEvent(TextView.java:6540)
       05-14 20:54:12.152: E/Database(243):     at android.view.View.dispatchTouchEvent(View.java:3709)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
       05-14 20:54:12.152: E/Database(243):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
       05-14 20:54:12.152: E/Database(243):     at android.os.Handler.dispatchMessage(Handler.java:99)
       05-14 20:54:12.152: E/Database(243):     at android.os.Looper.loop(Looper.java:123)
       05-14 20:54:12.152: E/Database(243):     at android.app.ActivityThread.main(ActivityThread.java:4363)
       05-14 20:54:12.152: E/Database(243):     at java.lang.reflect.Method.invokeNative(Native Method)
       05-14 20:54:12.152: E/Database(243):     at java.lang.reflect.Method.invoke(Method.java:521)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
       05-14 20:54:12.152: E/Database(243):     at dalvik.system.NativeStart.main(Native Method)
quame
  • 3
  • 3

1 Answers1

0

You need to enclose the string variable in single quotes in your query.

It should look like this:

G_NAME+" = '" + string + "'"

EDIT

This is incorrect, as you are trying to access the cursor without moving to the first record first:

int iLatitude = c.getColumnIndex(LATITUDE);
if (c !=null){
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
    latitudes.add(c.getString(iLatitude));
}

Try this:

c.moveToFirst();
int iLatitude = c.getColumnIndex(LATITUDE);
if (c !=null){
    while(c.isAfterLast == false){
        latitudes.add(c.getString(iLatitude));
        c.moveToNext();
    }
}

This also changes your loop, mostly in the interest of readability.

Barak
  • 16,318
  • 9
  • 52
  • 84
  • please i have done that but it is still throwing the error java.lang.NullPointerException – quame May 14 '12 at 11:16
  • Where? What line? I've found another issue I'll add to my answer in a bit, but it would generate an error, but not an NPE. – Barak May 14 '12 at 12:03
  • i have tried the fix by moving the cursor to first and looping with the while loop but its still gives the java.lang.NullPointerException. but its works when i used the primary key to query.please help me because i have to query a different column.(G_NAME column). – quame May 14 '12 at 14:21
  • please Barak im waiting for the fix that would not return NPE error.i seriously need help. – quame May 14 '12 at 18:31
  • Edit your question and post the logcat showing the error so we can see where it is happening – Barak May 14 '12 at 19:03
  • pls i have added the log cat. – quame May 15 '12 at 01:47