0

I am implementing an SQLite database and I can not figure out why some column values are null. Here is where I am writing to my SQLiteDatabase(db). The ArrayList DEFINITELY has the list of vehicles and each vehicle DEFINITELY has the correct values in it(Vehicle ID, address, city, state....). The table inside of the database also has the columns that i constructed(I checked this as well).

For some reason when I am doing a call like cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_CITY)) it is getting null but when I do the same call but to KEY_LAT, it works. (Vehicle ID, Lat and Lon work, but the others do not)..

public static void addVehicle(ArrayList<Vehicle> aList) {

        Database.openWritable();
        ContentValues values = new ContentValues();
        for(Vehicle vehicle : aList){

        values.put(KEY_VEHICLE_ID, vehicle.getvehicleID());  
        values.put(KEY_ADDRESS, vehicle.getaddress());
        values.put(KEY_CITY, vehicle.getcity());
        values.put(KEY_STATE, vehicle.getstate());

        values.put(KEY_LAT, vehicle.getLat());
    values.put(KEY_LON, vehicle.getLon());

        db.insert(TABLE_VEHICLE, null, values);
           }
    }

Additional Information/Question This do loop is happening like hundreds of times and i feel like it should only be happening for the number of rows i have, which is 14(i have 14 vehicles in the database). This may be affecting something? But i suspect not. So why is this happening? :

    String query = "SELECT * FROM " + DatabaseHandler.TABLE_VEHICLE;
    Cursor cursor = Database.listOfVehiclesDesired(query);

    String lat = "";
    String lon = "";
    String title = "";
    String snippet = "";


    if(cursor.moveToFirst()){
        do {

            lat = (cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_LAT)));
            lon = (cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_LON)));
            title = cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_VEHICLE_ID)) + " " +  cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_VEHICLE_NAME));
            snippet = "City:" + cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_CITY)) + ", State: " + cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_STATE));
            addMarkers(new LatLng(lat, lon), true, R.drawable.cap, title, snippet);
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

Superfluous(?) BACKGROUND INFROMATION: I am using a static class to access the SQLiteDatabase, also, this code of getting column index and such is in an AsyncTask.

4
  • 1
    You can set for each column of the table a column constraint not null when creating the table. addVehicle() will fail then when a null value should be inserted. So you can check if this is the problem Commented Jun 28, 2013 at 17:14
  • Is this a proper statement? "CREATE TABLE " + TABLE_VEHICLE + " (" + KEY_VEHICLE_ID + " TEXT NOT NULL, " +...repeat. I don't believe that it failed Commented Jun 28, 2013 at 17:38
  • can you think of a reason why some values are null? i am 100% sure that the information that I am putting in has values! somewhere the values must be getting lost or something? Commented Jun 28, 2013 at 17:47
  • There are many possibilities. You should use a debugger to watch exactly what is going on. If this doesn't help, show the code that calls addVehicle() Commented Jun 28, 2013 at 20:14

1 Answer 1

0

how you close cursor if cursor is closed ?!! you code :

   if (cursor != null && !cursor.isClosed()) {
    cursor.close();

try the code with just cursor.close();

Sign up to request clarification or add additional context in comments.

5 Comments

I think that the code is saying that if it isn't closed, then it should close. i tried it the way you said and it did not fix the problem :(
maybe you should add return; method after cursor.close(); to return somethengs
it is in a void method. The issue is higher up i believe
yeah it is but finaly, according to your discribe of the errors I thing " snippet = " is the cause try to edit it or do it in deferent way you know .
This code snippet is perfectly ok (except that the cursor != null check wouldn't be necessary in the context of the original code but it doesn't harm either)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.