简体   繁体   中英

Inserting data into a db from a list

I have a problem inserting data from a list into a db SQLite. Some element of the list are null and then when i'm going to take this element i see a null pointer exception. What i can do to store in db this data even though some are null??

This is the code:

private void insertDB() {
    for(int i = 0; i < listA.size(); i++) {
        int check = dbPF.createPF(listA.get(i).getA(), listA.get(i).getB(), listA.get(i).getC(), listA.get(i).getD());
    }
 }

public int createPF(int a, String b, String c, String d) {
    ContentValues initialValues = createContentValuesPF(a, b, c, d);
    return (int) database.insert(tableA, null, initialValues);
}

private ContentValues creaContentValuesPod(int a, String b, String c, String d) {
       ContentValues values = new ContentValues();
       values.put(key1, a);
       values.put(key2, b);
       values.put(key3, c);
       values.put(key4, d);
       return values;
}

Eclipse return a NullPointerException when one of the node of the list is null, but i want to store in db also null values. What i can do?

Thanks to all

First you need to make sure in your table creation statment doesn't have "not null" in it. When using the ContentValues just skip the fields you want to be null.

Check out this question. It will give you what you want. Insert Nulls in SQLite DB

If you are using a PreparedStatement you can insert null data to the database by leveraging the setNull API. Below is an example

            preparedStatement.setNull(parameterIndex,Types.DATE);

Check if its null and than put nulls into the DB.

private void insertDB() {
    String a = "";
    String b = "";
    String c = "";
    String d = "";
    for(int i = 0; i < listA.size(); i++) {
        if(listA.get(i) != null){
            a = listA.get(i).getA();
            b = listA.get(i).getB();
            c = listA.get(i).getC();
            d = listA.get(i).getD();   
        }
        int check = dbPF.createPF(a, b, c, d);
    }
 }

private ContentValues creaContentValuesPod(int a, String b, String c, String d) {
   ContentValues values = new ContentValues();
   values.put(key1, a);

   if(b.equals(""){
      values.put(key2, null);
   }else{
      values.put(key2, b);
   }
   if(c.equals(""){
      values.put(key3, null);
   }else{
      values.put(key3, c);
   }
   if(d.equals(""){
      values.put(key4, null);
   }else{
      values.put(key4, d);
   }
   return values;

}

I put an empty string. I have a question, why you have in a list Null-pointer-reference? It's not a good methodology.

When a list item is null , you cannot call getA() etc.

You need to do something else when you have a null item:

private void insertDB() {
    for(int i = 0; i < listA.size(); i++) {
        int check = dbPF.createPF(listA.get(i));
    }
}

public int createPF(MyObject item) {
    ContentValues initialValues = createContentValuesPF(item);
    return (int) database.insert(tableA, null, initialValues);
}

private ContentValues creaContentValuesPod(MyObject item) {
    ContentValues values = new ContentValues();
    if (item != null) {
        values.put(key1, item.getA());
        values.put(key2, item.getB());
        values.put(key3, item.getC());
        values.put(key4, item.getD());
    } else {
        values.putNull(key1); // or whatever value(s) you want
    }
    return values;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM