0

My app craches when i try to update my database it gives me the following problem

attempt to invoke virtual method 'android.database.cursor com.leoni.bd.Gestion_db.FindDate(java.lang.String)' on a null object reference

i couldn't find the null object i tried many things but it didn't work ! i need help please !

this is my Gestion_db.java class

 private SQLiteDatabase _myDbm;

public Gestion_db(Context pContext) {

    SqliteCreator s = new SqliteCreator(pContext, Stat.DB_NAME, null, 1);

    _myDbm = s.getWritableDatabase();
}

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

public Cursor FindDate(String Attribute) {
        String query = "SELECT * FROM " +Stat.TABLE_NAME +" WHERE ? LIKE '%?%' ";
        return _myDbm.rawQuery(query, new String[] {Stat.COL_DATE,Attribute});

    }

this is the method from my Controle.java activity wich contain the cursor

//header of the activity 
private Gestion_db _myGestionDB;
private String _myRecognizedText = null;

// Mise à jour de la base de données quelque soit l'action
private void MiseAJour() {

    String dateCourante = new SimpleDateFormat("yyyy:MM:dd",Locale.getDefault()).format(new Date());
    Boolean existe=false;

    Cursor c = _myGestionDB.FindDate(dateCourante);

    if (c.getCount() != 0) {

        c.moveToFirst();

        while (!c.isAfterLast()) {


            String ldate = c.getString(c.getColumnIndex(Stat.COL_DATE));
            String lMatricule = c.getString(c.getColumnIndex(Stat.COL_TEXTE_OCR));

            if (ldate.equals(dateCourante)&& lMatricule.equals(_myRecognizedText)) {
                existe=true;
                break;

            }
            c.moveToNext();

        }



    }

    if (existe){
        UpdateHeure(_myHeure);

    }else{
        AddVoyage();
    }

}

this ic Stat.java class wich contains some Strings

public class Stat {
public static final String DB_NAME = "leoni.db";
public static final String URL_CHECK = "http://192.168.1.6/check.php";
public static final String GET_URL = "http://192.168.1.6/getChauffeurs.php";
public static final String COL_ID = "_id";

// Gestion des déplacements
public static final String TABLE_NAME = "gestion_des_deplacements";
public static final String COL_TEXTE_OCR = "texte_ocr";
public static final String COL_DATE = "date";
public static final String COL_HEURE_DEPART = "heure_depart";
public static final String COL_HEURE_ARRIVEE = "heure_arrive";
public static final String CREATE_TABLE_DEPLACEMENTS = "CREATE TABLE "
        + Stat.TABLE_NAME + " (" + Stat.COL_ID
        + " INTEGER PRIMARY KEY autoincrement," + Stat.COL_TEXTE_OCR
        + " VARCHAR(40)" + "," + Stat.COL_CHAUFFEUR + " VARCHAR(50)" + ","
        + Stat.COL_DATE + " VARCHAR(50)" + "," + Stat.COL_HEURE_DEPART
        + " VARCHAR(30)" + "," + Stat.COL_HEURE_ARRIVEE + " VARCHAR(30));";

// Gestion des chauffeurs
public static final String COL_MATRICULE = "matricule";
public static final String COL_CHAUFFEUR = "chauffeur";

}
user 007
  • 821
  • 10
  • 31

2 Answers2

0

Your stacktrace gives you all the needed information:

com.leoni.bd.Gestion_db.FindDate(java.lang.String)' on a null object reference

What is says is that object of type Gestion_db is null. So you look in your code for this object, and you have only one instance of it _myGestionDB, as noted by @ρяσѕρєя.

AndroidEx
  • 15,524
  • 9
  • 54
  • 50
0

The error message identifies the method that is the subject of the problematic invocation as com.leoni.bd.Gestion_db.FindDate(java.lang.String). It follows, therefore, that the type of the expression on which the invocation is performed must be com.leoni.bd.Gestion_db or one of its subtypes.

The only candidate in the code you posted is instance variable _myGestionDB. The code you posted does not give any particular reason to think the value would be non-null. Instance variables of reference type are initialized to null by default if they have no initializer. If you fail to set its value to something else prior to invoking the method you show, then it will still be null when the method invocation attempt occurs.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157