NullPointerException插入到SQLite数据库中?

时间:2015-01-28 15:07:14

标签: java android nullpointerexception

当我尝试插入SQLite数据库时,我不知道为什么会抛出此异常:

  

01-28 14:00:36.841:E / AndroidRuntime(1505):致命异常:主要   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)   ...

这是我的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 product 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 product 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;
    }
}

我调用片段AddStockageFragment中的方法,如下所示:

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

Stockage实体具有与这些字段对应的属性:

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

2 个答案:

答案 0 :(得分:0)

您发布的堆栈跟踪片段似乎表明问题发生在AddStockageFragment.java的第209行,我将其作为这一行:

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

请注意,它似乎并不表示方法内部存在问题,而是首先在调用方法时出现问题。有两种与此相符的基本可能性:

  1. 变量mStockageDaonull
  2. stockqteseulstockLocalStock中的一个是null
  3. 只有当三个变量中的一个或多个属于包装类型(例如Double)时,才可能使用替代(2);任何具有基本类型(例如double)的都不能是null。无论哪种方式,问题似乎都在AddStockageFragment,而不在StockageDAO

答案 1 :(得分:0)

您尚未初始化变量mStockageDao

onCreate()方法中执行此操作:

StockageDAO mStockageDao = new mStockageDao(AddStockageFragment.this);

在代码之前:

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

称之为

mStockageDao.open();

注意: 完成查询数据库后,请致电mStockageDao.close();