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.
not nullwhen creating the table.addVehicle()will fail then when a null value should be inserted. So you can check if this is the problemaddVehicle()