mercredi 28 janvier 2015

NullPointerException probleme insert sqlite database?

i don't know what is the problem went i try to insert on sqlite database


01-28 14:00:36.841: E/AndroidRuntime(1505): FATAL EXCEPTION: main 01-28 14:00:36.841: E/AndroidRuntime(1505): java.lang.NullPointerException 01-28 14:00:36.841: E/AndroidRuntime(1505): at com.smartfridge.fragment.AddStockageFragment.onClick(AddStockageFragment.java:209).....


this is my StockageDAO


public class StockageDAO {


public static final String TAG = "StockageDAO";



private Context mContext;

// Database fields
private SQLiteDatabase mDatabase;
private DataBaseHelper mDbHelper;
private String[] mAllColumns = {
DataBaseHelper.ID_STOCKAGE,
DataBaseHelper.QTE_STOCKAGE,
DataBaseHelper.EXP_DATE_STOCKAGE,
DataBaseHelper.SEUIL_STOCKAGE,
DataBaseHelper.LOCALISATION_STOCKAGE,
DataBaseHelper.PRODUIT_IDForing_CAT_STOCKAGE };



public StockageDAO(Context context) {
mDbHelper = new DataBaseHelper(context);
this.mContext = context;
// open the database
try {
open();
}
catch(SQLException e) {
Log.e(TAG, "SQLException on openning database " + e.getMessage());
e.printStackTrace();
}
}

public void open() throws SQLException {
mDatabase = mDbHelper.getWritableDatabase();
}

public void close() {
mDbHelper.close();
}

private static final SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd", Locale.ENGLISH);


public Stockage createStockage(double qte_stockage, String exp_date_stockage, double seul_stockage, int localisation_stockage, int ForingIdStock) {
ContentValues values = new ContentValues();


values.put(DataBaseHelper.QTE_STOCKAGE, qte_stockage);
values.put(DataBaseHelper.EXP_DATE_STOCKAGE, exp_date_stockage);
values.put(DataBaseHelper.SEUIL_STOCKAGE, seul_stockage);
values.put(DataBaseHelper.LOCALISATION_STOCKAGE, localisation_stockage);
values.put(DataBaseHelper.PRODUIT_IDForing_CAT_STOCKAGE, ForingIdStock);




long insertId = (int) mDatabase.insert(DataBaseHelper.STOCKAGE_TABLE, null, values);
Cursor cursor = mDatabase.query(DataBaseHelper.STOCKAGE_TABLE,
mAllColumns, DataBaseHelper.ID_STOCKAGE + " = " + insertId, null, null, null, null);
cursor.moveToFirst();
Stockage newStockage = cursorToStockage(cursor);
cursor.close();
return newStockage;


}




public void deleteStockage(Stockage stockage) {
int id = stockage.getIdStock();
System.out.println("the deleted employee has the id: " + id);
mDatabase.delete(DataBaseHelper.STOCKAGE_TABLE, DataBaseHelper.ID_STOCKAGE + " = " + id, null);
}

public List<Stockage> getAllStockages() {
List<Stockage> listStockages = new ArrayList<Stockage>();

Cursor cursor = mDatabase.query(DataBaseHelper.STOCKAGE_TABLE,
mAllColumns, null, null, null, null, null);

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Stockage stockage = cursorToStockage(cursor);
listStockages.add(stockage);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return listStockages;
}

public List<Stockage> getStockagesByProduit(int ForingIdStock) {
List<Stockage> listStockages = new ArrayList<Stockage>();

Cursor cursor = mDatabase.query(DataBaseHelper.STOCKAGE_TABLE, mAllColumns
, DataBaseHelper.PRODUIT_IDForing_CAT_STOCKAGE + " = ?",
new String[] { String.valueOf(ForingIdStock) }, null, null, null);

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Stockage stockage = cursorToStockage(cursor);
listStockages.add(stockage);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return listStockages;
}

public List<IngredientStockage> getStockagesByProduitBetween(int ForingId) {
List<IngredientStockage> listIngredientStockages = new ArrayList<IngredientStockage>();
Cursor cursor = mDatabase.rawQuery("SELECT "+DataBaseHelper.STOCKAGE_TABLE+"."




+DataBaseHelper.ID_STOCKAGE+", "+DataBaseHelper.PRODUIT_TABLE+"."
//+DataBaseHelper.ID_PRODUIT+", "+DataBaseHelper.PRODUIT_TABLE+"."

+DataBaseHelper.NAME_PRODUIT+", "+DataBaseHelper.PRODUIT_TABLE+"."
+DataBaseHelper.TYPE_PRODUIT+", "+DataBaseHelper.STOCKAGE_TABLE+"."
+DataBaseHelper.QTE_STOCKAGE+", "+DataBaseHelper.STOCKAGE_TABLE+"."
+DataBaseHelper.SEUIL_STOCKAGE+", "+DataBaseHelper.STOCKAGE_TABLE+"."
+DataBaseHelper.EXP_DATE_STOCKAGE+", "+DataBaseHelper.STOCKAGE_TABLE+"."
+DataBaseHelper.LOCALISATION_STOCKAGE+", "+DataBaseHelper.STOCKAGE_TABLE+"."
//+DataBaseHelper.PRODUIT_IDForing_CAT_STOCKAGE+", "
+DataBaseHelper.PRODUIT_IDForing_CAT_STOCKAGE+


" FROM "+ DataBaseHelper.STOCKAGE_TABLE + "," + DataBaseHelper.PRODUIT_TABLE + " where " + DataBaseHelper.STOCKAGE_TABLE + ".ForingIDProd = " + DataBaseHelper.PRODUIT_TABLE + ".idProd " + " and ForingIdCat = " + ForingId,new String [] {} );

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
IngredientStockage ingredientstockage = cursorToIngredientStockage(cursor);
listIngredientStockages.add(ingredientstockage);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return listIngredientStockages;
}
////////////////////





@SuppressLint("SimpleDateFormat")
public List<IngredientStockage> getStockagesByProduitBetweenshoping() {
List<IngredientStockage> listIngredientStockages = new ArrayList<IngredientStockage>();

Calendar c = Calendar.getInstance();

SimpleDateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate1 = df1.format(c.getTime());

Cursor cursor = mDatabase.rawQuery("SELECT "+DataBaseHelper.STOCKAGE_TABLE+"."

+DataBaseHelper.ID_STOCKAGE+", "+DataBaseHelper.PRODUIT_TABLE+"."
+DataBaseHelper.NAME_PRODUIT+", "+DataBaseHelper.PRODUIT_TABLE+"."
+DataBaseHelper.TYPE_PRODUIT+", "+DataBaseHelper.STOCKAGE_TABLE+"."
+DataBaseHelper.QTE_STOCKAGE+", "+DataBaseHelper.STOCKAGE_TABLE+"."
+DataBaseHelper.SEUIL_STOCKAGE+", "+DataBaseHelper.STOCKAGE_TABLE+"."
+DataBaseHelper.EXP_DATE_STOCKAGE+", "+DataBaseHelper.STOCKAGE_TABLE+"."
+DataBaseHelper.LOCALISATION_STOCKAGE+", "+DataBaseHelper.STOCKAGE_TABLE+"."
//+DataBaseHelper.PRODUIT_IDForing_CAT_STOCKAGE+", "
+DataBaseHelper.PRODUIT_IDForing_CAT_STOCKAGE+


" FROM "+ DataBaseHelper.STOCKAGE_TABLE + "," + DataBaseHelper.PRODUIT_TABLE + " where (" + DataBaseHelper.STOCKAGE_TABLE + ".ForingIDProd = " + DataBaseHelper.PRODUIT_TABLE + ".idProd) and ( ( "+DataBaseHelper.STOCKAGE_TABLE+"." +DataBaseHelper.QTE_STOCKAGE+" <= "+DataBaseHelper.STOCKAGE_TABLE+"." +DataBaseHelper.SEUIL_STOCKAGE+") || ("+DataBaseHelper.STOCKAGE_TABLE+"." +DataBaseHelper.EXP_DATE_STOCKAGE+" <= "+formattedDate1+")"+")" ,new String [] {} );

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
IngredientStockage ingredientstockage = cursorToIngredientStockage(cursor);
listIngredientStockages.add(ingredientstockage);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return listIngredientStockages;
}


public List<Produit> getProduitsOfStockage(int ForingIdprod) {
List<Produit> listProduits = new ArrayList<Produit>();

Cursor cursor = mDatabase.query(DataBaseHelper.PRODUIT_TABLE, mAllColumns
, DataBaseHelper.PRODUIT_IDForing_CAT_STOCKAGE + " = ?",
new String[] { String.valueOf(ForingIdprod) }, null, null, null);

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Produit produit = cursorToProduit(cursor);
listProduits.add(produit);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return listProduits;
}

private Produit cursorToProduit(Cursor cursor) {
Produit produit = new Produit();
produit.setIdProd(cursor.getInt(0));
produit.setNameProd(cursor.getString(1));
produit.setTypeProd(cursor.getString(2));

// get The company by id
int ForingIdCat = cursor.getInt(3);
CategorieDAO dao = new CategorieDAO(mContext);
Categorie categorie = dao.getCategorieById(ForingIdCat);
if(categorie != null)
produit.setCategorie(categorie);

return produit;
}

protected IngredientStockage cursorToIngredientStockage(Cursor cursor) {
IngredientStockage is = new IngredientStockage();
is.setId(cursor.getInt(0));
is.setNameIS(cursor.getString(1));
is.setTypeIS(cursor.getString(2));
is.setQteIS(cursor.getDouble(3));
is.setEXP_DateIS(cursor.getString(5));

is.setSeuilIS(cursor.getDouble(4));
is.setLocalisationIS(cursor.getInt(6));



// get The produit by id
int ForingIdprod = cursor.getInt(7);
ProduitDAO dao = new ProduitDAO(mContext);
Produit produit = dao.getProduitByForingId(ForingIdprod);
if(produit != null)
is.setProduit(produit);


return is;
}

protected Stockage cursorToStockage(Cursor cursor) {
Stockage stockage = new Stockage();
stockage.setIdStock(cursor.getInt(0));
stockage.setQteStock(cursor.getDouble(1));
stockage.setEXP_DateStock(cursor.getString(2));
stockage.setSeuilStock(cursor.getDouble(3));
stockage.setLocalisation(cursor.getInt(4));

// get The produit by id
int ForingIdprod = cursor.getInt(5);
ProduitDAO dao = new ProduitDAO(mContext);
Produit produit = dao.getProduitByForingId(ForingIdprod);
if(produit != null)
stockage.setProduit(produit);


return stockage;
}


}


and i call the methode in the fragment AddStockageFragment


as that :

Stockage createdStockage = mStockageDao.createStockage(stockqte,datestock,seulstock,LocalStock,1);


this is stockage entitie :



private int idStock;
private double QteStock;
private String EXP_DateStock;
private double SeuilStock;
private int Localisation;
private Produit produit; // rekation with table product


this for helpe :)


Aucun commentaire:

Enregistrer un commentaire