csv 파일에서 sqlite 데이터베이스로 몇 줄을 가져 오려고 할 때 널 포인터 예외가 발생합니다.csv에서 sqlite로 데이터를 가져온 후 null 포인터 예외가 발생했습니다.
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();
}
}
내가 Heres는 내 코드를 메서드 db.addProduct (...)
를 호출 할 때 나는 NPE를 얻고있다.
감사합니다.
'db'초기화에 관련 코드를 게시하고 db helper를 추가하십시오. – user2450263
게시 된 코드에 따르면'db'가 NULL이라고 말하고 싶습니다. 아니면 변수가 다른 곳에 할당되어 있습니까? 그렇다면 @ user2450263과 같은 코드를 게시하시기 바랍니다. – Martijn
[NullReferenceException은 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – germi