-4

I don't know why this exception is thrown when I try to insert into an 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 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;
    }
}

I invoke the method in the fragment AddStockageFragment, like so:

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

The Stockage entity has properties corresponding to these fields:

private int idStock;
private double QteStock;
private String EXP_DateStock;
private double SeuilStock;
private int Localisation;
private Produit produit; // Rekation with table product
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hani
  • 109
  • 3
  • 10
  • 2
    You should better format your code – Paul Lo Jan 28 '15 at 15:11
  • Find out what's null and make sure it isn't. Check out line 209 of your `AddStockageFragment` [sic]. – 323go Jan 28 '15 at 15:20
  • yes the line 209 is "createStockage" : this function is colled to add a new stockage, so the problem is on this function – Hani Jan 28 '15 at 15:27
  • Please add `AddStockageFragment` class code and point to line# 209 – Rohit5k2 Jan 28 '15 at 15:30
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Jan 28 '15 at 15:41
  • You are not initializing your variable `mStockageDao`. Initialize it before you use it. – Rohit5k2 Jan 28 '15 at 15:45

2 Answers2

0

The stack trace fragment you posted seems to indicate that the problem occurs at line 209 of AddStockageFragment.java, which I take to be this line:

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

Note that it does not appear to indicate a problem inside the method, but rather a problem invoking the method in the first place. There are two basic possibilities consistent with that:

  1. variable mStockageDao is null, or
  2. one of stockqte, seulstock, or LocalStock is null.

Alternative (2) is possible only if one or more of the three variables is of a wrapper type (e.g. Double); any that have primitive type (e.g. double) cannot be null. Either way, the problem appears to be in AddStockageFragment, not in StockageDAO.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • if i try to use a static values like this; Stockage createdStockage = mStockageDao.createStockage( 10.0, "28/01/2015", 2.0, 1, 1); it will return the same problem, – Hani Jan 28 '15 at 15:47
  • Then it is `mStockageDao` that is `null`. – John Bollinger Jan 28 '15 at 15:48
  • yes this is the problem thank you very much :) i must add this in the oncreate (... this.mStockageDao = new StockageDAO(getActivity()); thanks for all ;) – Hani Jan 28 '15 at 15:58
  • That's I have mentioned in my answer! I guess you didn't see that. – Rohit5k2 Jan 28 '15 at 16:00
  • @Rohit5k2, look who answered first, and tell me again who didn't see whose answer. – John Bollinger Jan 28 '15 at 16:10
  • @JohnBollinger both you have answer right but Rohitk2 is the first thx for all other especially both of you :) – Hani Jan 28 '15 at 19:11
0

You have not initialized your variable mStockageDao.

Do this in onCreate() method:

StockageDAO mStockageDao = new mStockageDao(AddStockageFragment.this);

And before the code:

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

call this

mStockageDao.open();

NOTE: When you are done querying your database, call mStockageDao.close();

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57