lundi 1 février 2016

Android Sqlite attempt to invoke virtual method/ null object reference

in my android application i have DBAdater as follows:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

//DB Connection file (SqLite Connection)
//Note: added_date phonegap DATETIME

public class DBAdapter 
{   
    public static final String KEY_ROWID = "_id";   //Table Fields ....
    private static final String TAG = "DBAdapter";    
    private static final String DATABASE_NAME = "KDHP";   //Database Name
    private static final int DATABASE_VERSION =41;
    public static String DateTime = null;


    private static final String DATABASE_CREATE_app_req =
        "create table app_req (_id integer primary key autoincrement, "
        + "req_no integer not null);";  

    //Login Master
    private static final String DATABASE_CREATE_login_mas =
            "create table login_mas (_id integer primary key autoincrement, userid integer not null, name text not null, "
            + "login_id text not null, password text not null, auth_key text not null, login_flag integer not null, "
            + "port_day integer null , port_month  integer null, port_year integer  null, " 
            + "user_type integer not null,sms_mode integer not null,dc_type integer not null);";   


    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE_app_req);
            db.execSQL(DATABASE_CREATE_login_mas);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {               
           Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS app_req");
            db.execSQL("DROP TABLE IF EXISTS login_mas");


            onCreate(db);           
        }
    } 

   //-------------------------- Insert ------------------------------------------------------------------------------




   //insert a User details into the sqlite database
   public long insertUser(int userid, String name, String login_id, String password)
   //  public long insertUser(int userid, String name, String login_id, String password, String auth_key,  int user_type,int sms_mode)
    {                   
        ContentValues initialValues = new ContentValues();
        initialValues.put("userid", userid);
        initialValues.put("name", name);
        initialValues.put("login_id", login_id);
        initialValues.put("password", password );
        initialValues.put("auth_key", "aaaa");
        initialValues.put("login_flag", 1);      
        initialValues.put("user_type", 1);
        initialValues.put("sms_mode", 1);
       initialValues.put("dc_type",1);
        return db.insert("login_mas", null, initialValues);
    }

   //insert a App Req details into the sqlite database
    public long insertAppReq(int req_no) 
    {                   
        ContentValues initialValues = new ContentValues();
        initialValues.put("req_no", req_no);
        return db.insert("app_req", null, initialValues);    
    }



   //-------------------------- Insert Ends Here------------------------------------------------------------------------------



   //-------------------------- Update ------------------------------------------------------------------------------



   //Update LoginFlag Status    
    public boolean updateLoginStatus(int rowId, int login_flag) 
    {
        ContentValues args = new ContentValues();
        args.put("login_flag", login_flag);
        return db.update("login_mas", args, 
                         null, null) > 0;
    }

    //Update ReqNo in App_Req
    public boolean updateAppReq(int rowId, int req_no) 
    {
        ContentValues args = new ContentValues();
        args.put("req_no", req_no);
        return db.update("app_req", args, 
                         null, null) > 0;
    }


  //Update Port Day, Month and Year   
    public boolean updateLoginDate(int rowId, int port_day, int port_month, int port_year) 
    {
        ContentValues args = new ContentValues();
        args.put("port_day", port_day);
        args.put("port_month",port_month);
        args.put("port_year", port_year);         
        return db.update("login_mas", args, 
                         null, null) > 0;
    }




  //-------------------------- Update Ends Here------------------------------------------------------------------------------




  //-------------------------- Retrive Data ------------------------------------------------------------------------------
  //---retrieves user details and to  check whether user is already login then go to main form else go to login form to login..
   //userid is server userid, login_id is  used to do Login
    public Cursor getUser() 
    {
        return db.query("login_mas", new String[] {
                "_id", 
                "userid",
                "name",
                "login_id",
                "password",
                "auth_key",
                "login_flag",
                "port_day",
                "port_month",
                "port_year",
                "user_type"}, 
                null, 
                null, 
                null, 
                null, 
                "name");                       
    }


  //-------------------------- Retrive Data ------------------------------------------------------------------------------  

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();        
    }

  //---Truncate table (delete all record from selected table), if already exists---
    public void truncateTable(String TableName) 
    {
        try{
        db.execSQL("DELETE FROM "+TableName);
        }
        catch(Exception e)
        {
            System.out.println("error>>>>>>>>>>>>> :" + e.toString());
        }
    }


    //executing query and it returns fields which are specified in the query ..........
    public Cursor getQueryResult(String MY_QUERY) throws SQLException 
    {   
        try {
            return db.rawQuery(MY_QUERY, null);    
        } catch (Exception e) {
            Log.e("MYAPP", "exception", e);
        }
        return null;       

    }    

}

I get java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference on

db.insertUser(json_data.getInt("id"), json_data.getString("name"), json_data.getString("login_id") ,password );

and

db.truncateTable("login_mas");

I get error on all DB operations and application crashes...

Please help as soon as possible...

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire