Search code examples
androidjackcess

jackcess empty field from array to database exception


I've used this code in order to pass data from access db to an array

    try {
    for(i=0;i<numfilas;i++){
    HashMap<Object, ?> rowdata = new HashMap<Object, Object>(cur.getNextRow());
    for(j=0;j<numcolumnas;j++){

        try
        {
            //get the value at this cursor position
            datos[posicion]=rowdata.get(nombrecolumnas[j]).toString();
        }
        catch(NullPointerException e)
        {
            //if it's null, just add the empty string
            datos[posicion] = "";
        }

        posicion++;

    }
    }posicion=0;
} catch (IOException e) {

as you can see when there is an empty field in DB then I put an empty string in my array, the problem comes when I want to write data back from the array to the DB, like this:

 int j = 0;
cur.reset();
Map<String, Object> myMap = new HashMap<String, Object>();
try {

    while(j<32){
    cur.moveToNextRow();
    myMap.clear();
    for (int i = 0; i < numcolumnas; i++) {
        myMap.put(nombrecolumnas[i], datos[j]);
        j++;
    }
    cur.updateCurrentRowFromMap(myMap);
    }

} catch (IOException e) {
    // TODO Auto-generated catch block

    //e.printStackTrace();
}

then I get this error

Java.lang.NumberFormatException: Invalid Double: ""

if the column datatype of the database is TEXT then nothing happens but if the datatype is long or double then I get that error, how can I solve this?


Solution

  • Your code to put stuff into the array converts null values to zero-length strings, so perhaps your code to pull stuff out of the array needs to do the reverse, something like this:

    myMap.put(nombrecolumnas[i], "".equals(datos[j]) ? null : datos[j]);