NullPointerException 插入 SQLite 数据库?

NullPointerException inserting into an SQLite database?

我不知道为什么当我试图插入一个 SQLite 数据库时抛出这个异常:

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) ...

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

您发布的堆栈跟踪片段似乎表明问题出现在 AddStockageFragment.java 的第 209 行,我认为是这一行:

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

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

  1. 变量mStockageDaonull,或者
  2. stockqteseulstockLocalStock 之一是 null

仅当三个变量中的一个或多个是包装器类型(例如Double)时,备选方案(2)才有可能;任何具有原始类型(例如 double)的都不能是 null。不管怎样,问题似乎出在 AddStockageFragment,而不是 StockageDAO.

你还没有初始化你的变量mStockageDao

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

StockageDAO mStockageDao = new mStockageDao(AddStockageFragment.this);

在代码之前:

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

称之为

mStockageDao.open();

注意: 查询完数据库后,调用 mStockageDao.close();