vendredi 28 août 2015

Java.lang.nullpointerexception error with sqlite database [duplicate]

This question already has an answer here:

I try create the database tables by execute the sql code but when run project, the tables is not create.database class is DB_Nabege_helper and activity is Mainactivity.

public class DB_Nabege_helper extends SQLiteOpenHelper {

    public final String DBpath = "/data/data/com.example.nabege.nabege/databases/";

    private static String DBname = "DB_Nabege.db";
    private SQLiteDatabase Nabege_sqldb;
    private static Context Nabege_context;
    private SQLiteDatabase db;

    public static final String nameCollection = "name_collection";

    public static final String nameTableCollection = "tbl_collection";


    public DB_Nabege_helper(Context context) {
        super(context, "DB_Nabege", null, 1);
        Nabege_context = context;
    }


    public void open() throws SQLException {

        try {
            SQLiteDatabase.openDatabase(DBpath + DBname, null, SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLException e) {
            Log.d("log", "open:::" + e.toString());
            //Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }
    }


    private Context getApplicationContext() {
            return null;
    }



    @Override
    public synchronized void close() {
        if (Nabege_sqldb != null) {
            Nabege_sqldb.close();
            super.close();
        }

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }
           @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }



    private Boolean checkDataBase()  throws SQLiteException{
        SQLiteDatabase checkableDatabase = null;
        try {
            checkableDatabase = SQLiteDatabase.openDatabase(DBpath + DBname,
                    null, SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e)
        {
            //Log.d("log","checkDataBase:::"+ e.toString());
        }

        if (checkableDatabase != null) {
            checkableDatabase.close();
        }
        return checkableDatabase != null;
    }


    public void createDataBase() throws SQLException {
        if (!checkDataBase())
        {
            this.getWritableDatabase();
           try {
               db.execSQL("CREATE TABLE [tbl_collection] (  [id]    integer PRIMARY KEY AUTOINCREMENT NOT NULL, [name_collection]   nvarchar(256) NOT NULL COLLATE NOCASE);"+
                       "CREATE TRIGGER [fkd_tbl_Subject_id_collection_tbl_collection_id] Before Delete ON [tbl_collection] BEGIN SELECT RAISE(ROLLBACK, 'delete on table tbl_collection violates foreign key constraint fkd_tbl_Subject_id_collection_tbl_collection_id') WHERE (SELECT id_collection FROM tbl_Subject WHERE id_collection = OLD.id) IS NOT NULL;  END; "+
                       "CREATE TABLE [tbl_Subject] (    [id]    integer PRIMARY KEY AUTOINCREMENT NOT NULL, [id_collection] integer NOT NULL,   [name_subject]  nvarchar(256) NOT NULL COLLATE NOCASE,    FOREIGN KEY ([id_collection])        REFERENCES [tbl_collection]([id])); "+
                       "CREATE TRIGGER [fki_tbl_Subject_id_collection_tbl_collection_id] Before Insert ON [tbl_Subject] BEGIN SELECT RAISE(ROLLBACK, 'insert on table tbl_Subject violates foreign key constraint fki_tbl_Subject_id_collection_tbl_collection_id') WHERE (SELECT id FROM tbl_collection WHERE id = NEW.id_collection) IS NULL;  END;  "+
                       "CREATE TRIGGER [fku_tbl_Subject_id_collection_tbl_collection_id] Before Update ON [tbl_Subject] BEGIN SELECT RAISE(ROLLBACK, 'update on table tbl_Subject violates foreign key constraint fku_tbl_Subject_id_collection_tbl_collection_id') WHERE (SELECT id FROM tbl_collection WHERE id = NEW.id_collection) IS NULL;  END;  "+
                       "CREATE TRIGGER [fkd_tbl_Subject_Property_id_subject_tbl_Subject_id] Before Delete ON [tbl_Subject] BEGIN SELECT RAISE(ROLLBACK, 'delete on table tbl_Subject violates foreign key constraint fkd_tbl_Subject_Property_id_subject_tbl_Subject_id') WHERE (SELECT id_subject FROM tbl_Subject_Property WHERE id_subject = OLD.id) IS NOT NULL;  END;"+
                       "CREATE TABLE [tbl_Subject_Property] (   [id]    integer PRIMARY KEY AUTOINCREMENT NOT NULL, [id_subject]    integer NOT NULL,   [text]  nvarchar(512) COLLATE NOCASE,   [picture]   nvarchar(512) COLLATE NOCASE,   [voice] nvarchar(512) COLLATE NOCASE,   [video] nvarchar(512) COLLATE NOCASE,    FOREIGN KEY ([id_subject])        REFERENCES [tbl_Subject]([id]));"+
                       "CREATE TRIGGER [fki_tbl_Subject_Property_id_subject_tbl_Subject_id] Before Insert ON [tbl_Subject_Property] BEGIN SELECT RAISE(ROLLBACK, 'insert on table tbl_Subject_Property violates foreign key constraint fki_tbl_Subject_Property_id_subject_tbl_Subject_id') WHERE (SELECT id FROM tbl_Subject WHERE id = NEW.id_subject) IS NULL;  END;"+
                       "CREATE TRIGGER [fku_tbl_Subject_Property_id_subject_tbl_Subject_id] Before Update ON [tbl_Subject_Property] BEGIN SELECT RAISE(ROLLBACK, 'update on table tbl_Subject_Property violates foreign key constraint fku_tbl_Subject_Property_id_subject_tbl_Subject_id') WHERE (SELECT id FROM tbl_Subject WHERE id = NEW.id_subject) IS NULL;  END;");

           } catch (SQLException e) {
                Log.d("log","createDataBase:::"+ e.toString());
                //Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_LONG).show();
           }
        }
   }



    private void copyDataBase() throws IOException {

        InputStream myInputStream = Nabege_context.getAssets().open(DBname);

        OutputStream myOutputStream = new FileOutputStream(DBpath + DBname);
        byte[] buffer = new byte[1024];
        int Length;
        while ((Length = myInputStream.read(buffer)) > 0) {
            myOutputStream.write(buffer, 0, Length);
        }
        myOutputStream.flush();
        myOutputStream.close();
        myInputStream.close();
    }



    public void add_collection(String nameCollection) {
        ContentValues cv = new ContentValues();
        cv.put(DB_Nabege_helper.nameCollection, nameCollection);
        SQLiteDatabase db = this.getWritableDatabase();
        db.insert(nameTableCollection, null, cv);

    }


}

in this codes exist my sql query that have no error.I execute this query in sql expert program.

mainactivity codes:

public class MainActivity extends Activity {
    private Button btnExitButton;
    private Button btnHelp;
    private Button btnAboutUs;
    private Button btnStart;
    private DB_Nabege_helper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btnExitButton = (Button) findViewById(R.id.btn_exit);
        btnExitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        btnHelp = (Button) findViewById(R.id.btn_help);
        btnHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent goToHelpActivity = new Intent(MainActivity.this,
                        help_activity.class);
                startActivity(goToHelpActivity);
            }
        });

        btnAboutUs = (Button) findViewById(R.id.btn_about_us);
        btnAboutUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent goToAboutUsActivity = new Intent(MainActivity.this,
                        about_us_activity.class);
                startActivity(goToAboutUsActivity);
            }
        });

        btnStart = (Button) findViewById(R.id.btn_start);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent goToStartActivity = new Intent(MainActivity.this,
                        collection__list_activity.class);
                startActivity(goToStartActivity);
            }
        }); 
        db = new DB_Nabege_helper(this);
        try {
            db.createDataBase();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            Log.d("log", "are exist data base:::" + e.toString());
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Aucun commentaire:

Enregistrer un commentaire