将数据从csv导入sqlite后出现空指针异常

时间:2014-04-24 11:53:39

标签: android sqlite csv

尝试将几行从csv文件导入sqlite数据库时,我得到一个空指针异常。当我调用方法db.addProduct(...)

时,我得到一个npe

继承我的代码:

  public class ImportDataHandler extends Activity {

    protected File file;
    protected DatabaseHandler db;
    private ArrayList<HashMap<String, Object>> mylist;
    protected ListView listView;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.importer);
      importData();
      listView = (ListView) findViewById(R.id.allProdList);
      mylist = new ArrayList<HashMap<String, Object>>();
      db = new DatabaseHandler(this); 

      if (db.getAllProducts().isEmpty()) {
          Log.i(null, "This is null");
      }
      if (!db.getAllProducts().isEmpty()){
         Log.i(db.getAllProducts().toString(), "Products");
          for (Product p : db.getAllProducts()) {
              HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("Product", p._name);
                map.put("Description", p._description);
                mylist.add(map);
            }
            ListAdapter adapter = new SimpleAdapter(this, mylist,
                    R.layout.list_layout, new String[] { "Product", "Description"},
                    new int[] { R.id.prod_name, R.id.prod_desc});


            listView.setAdapter(adapter);
      }
     }

     public void importData() {

         File exportDir = new File(Environment.getExternalStorageDirectory(), "");

         if (!exportDir.exists()) {
             exportDir.mkdirs();
         }

         file = new File(exportDir, "ProductCSV.csv");

         try {

             CSVReader reader = new CSVReader(new FileReader(file));
             String [] nextLine;

             try {

                 while ((nextLine = reader.readNext()) != null) {

                    String skuCode = nextLine[0];
                    String name = nextLine[1];
                    String description = nextLine[2];
                    String price = nextLine[3];
                    String image = nextLine[4];
                    String category = nextLine[5];

                    if(skuCode.equalsIgnoreCase("Sku Code")) {

                    } else {
                        db.addProduct(new Product(skuCode, name, description, Integer.parseInt(price), image, Integer.parseInt(category))); /** This where the npe is occuring */
                    }

                 }
             } catch (IOException e) {
                 e.printStackTrace();
             }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

     }

}

数据库助手:

public class DatabaseHandler extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "catalogueManager";

    private static final String TABLE_PRODUCTS = "products";

    // Product Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_PRODUCT_SKUCODE = "skuCode";
    private static final String KEY_PRODUCT_NAME = "name";
    private static final String KEY_PRODUCT_DESCRIPTION = "description";
    private static final String KEY_PRODUCT_PRICE = "price";    
    private static final String KEY_PRODUCT_IMAGE = "image";
    private static final String KEY_PRODUCT_CATEGORY = "category";

    // Create table Product
    private static final String CREATE_PRODUCT_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," 
            + KEY_PRODUCT_SKUCODE + " TEXT,"
            + KEY_PRODUCT_NAME + " TEXT,"
            + KEY_PRODUCT_DESCRIPTION + " TEXT," 
            + KEY_PRODUCT_PRICE + " INTEGER," 
            + KEY_PRODUCT_IMAGE + " TEXT," 
            + KEY_PRODUCT_CATEGORY + " INTEGER" 
            + ")";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_PRODUCT_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    public void addProduct(Product product) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_PRODUCT_SKUCODE, product.getSkuCode()); 
        values.put(KEY_PRODUCT_NAME, product.getName()); 
        values.put(KEY_PRODUCT_DESCRIPTION, product.getDescription()); 
        values.put(KEY_PRODUCT_PRICE, product.getPrice()); 
        values.put(KEY_PRODUCT_IMAGE, product.getImage()); 
        values.put(KEY_PRODUCT_CATEGORY, product.getCategory()); 

        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
}


}

非常感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分:1)

在您的代码中,您似乎在调用&#34; importData&#34;在初始化&#34; db&#34;之前,它之所以为空。