2014-04-24 41 views
0

當我試圖從csv文件導入幾行到sqlite數據庫時,出現空指針異常。我得到一個NPE,當我調用一個方法db.addProduct(...)將數據從csv導入sqlite後出現空指針異常

繼承人我的代碼:

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(); 
} 


} 

任何幫助將大大讚賞。

謝謝。

+2

發佈與'db'初始化相關的代碼,並添加產品,db helper – user2450263

+1

根據發佈的代碼,我會說'db'是NULL。或者是在其他地方分配的變量?如果是這樣,請發佈該代碼,如@ user2450263所示。 – Martijn

+0

[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – germi

回答

1

在你的代碼中,似乎你在初始化「db」之前調用了「importData」,它之所以爲空。

+0

哦,謝謝!我不知道我是如何錯過的!非常感謝! –

+0

不客氣,有時我們的代碼太多以至於發現基本的白癡錯誤;) – tanou