Search code examples
javaandroidsqlitecursor

Android SQLite error on cursor


I'm currently working on an android project and am stuck on this method. The cursor query works but anything after that does not. If I take out the 'returnCompany' method it runs so I'm guessing the error is inside the cursor query. The method is taking in a string parameter called 'companyNameParemeter'. FYI Android Studio is the text editor I'm using.

SQLiteDatabase db = this.getReadableDatabase();
    String returnString = "";

    Cursor cursor = db.rawQuery("Select * from " + CONTANT_INFO_AND_GPS_TABLE + " where " +
   KEY_COMPANY_NAME + " = ? ", new String[] {companyNameParamater});


    company returnCompany = new company(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
            cursor.getString(2), cursor.getString(3),Double.parseDouble(cursor.getString(4) ),
             Double.parseDouble(cursor.getString(5)));

    /* returnString = ("Company is:\t" + returnCompany.getCompanyName() + ",\nContact name:\t" +
                returnCompany.getContactName() + ",\tContact No:\t" + returnCompany.getContactNumber()
                + ",\nGPS:\tLat:\t"
        + returnCompany.getGPS_Latitude() + "\tLong:\t" + returnCompany.getGPS_Longitude() );*/

        return returnString;

I checked LogCat and the only relevant error I could find was this

04-04 12:06:38.237 2032-1714/? E/GCoreUlr: Received null location result

Solution

  • Try using

    cursor.moveToFirst()
    

    Right AFTER creating and filling it with db.rawquery or wichever call you use to get your data from your DB,

    It will make sure your cursor is pointing at the first element you received, i may be wrong but i remember it's always pointing at the end of the data it contains when you fill it.