Jump to content

Recommended Posts

Bonjour,

je suis actuellement en bts iris 2eme années et je dois réaliser un projet qui consiste pour ma part de creer une BDD et ensuite de générer un devis avec les info presente dans celle ci.

J'ai donc créé une bdd avec firefox que j'ai copier dans le répertoire de mon activité android(je suis sous androidstudio).

Voici mon code et ensuite mon erreur : 

package eri.bdd;
 
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
 
public class DataBaseHelper extends SQLiteOpenHelper {
    private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
    //destination path (location) of our database on device
    private static String DB_PATH = "/data/data/eri.bdd/databases/";
    private static String DB_NAME = "ipodr.sqlite";// Database name
    private SQLiteDatabase myDataBase;
    private static String path = DB_PATH+DB_NAME;
    private final Context myContext;
 
    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);// 1? its Database Version
       // if(android.os.Build.VERSION.SDK_INT >= 17){
       //     DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
       // }
       // else
       // {
       //     DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
       // }
        this.myContext = context;
    }
 
    /**
     * Creates a empty database on the system and rewrites it with your own database.
     */
    public void createDataBase() throws IOException {
 
        boolean isdbExist = checkDataBase();
 
        if (!isdbExist) try {
            this.getReadableDatabase();
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Probleme de copie de la base de données");
        }
 
    }
 
 
    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     *
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase() {
        // TODO Auto-generated method stub
        SQLiteDatabase db = null;
        try {
            db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
            if (db != null)
                db.close();
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
 
        return db != null ? true : false;
    }
 
    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     */
    private void copyDataBase() throws IOException {
        // TODO Auto-generated method stub
        InputStream inputStream = myContext.getAssets().open(DB_NAME);
        OutputStream outputStream = new FileOutputStream(path);
 
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }
 
    public void openDataBase() throws SQLException {
        myDataBase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
    }
 
    @@override
    public synchronized void close() {
 
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }
 
 
    @@override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
 
    }
 
    @@override
    public void onUpgrade(SQLiteDatabase arg0, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
 
    }
}
 
 
 
02-05 06:03:28.990      858-858/eri.bdd D/AndroidRuntime﹕ Shutting down VM
02-05 06:03:28.990      858-858/eri.bdd W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41465700)
02-05 06:03:29.060      858-858/eri.bdd E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{eri.bdd/eri.bdd.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:235)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
            at eri.bdd.DataBaseHelper.createDataBase(DataBaseHelper.java:46)
            at eri.bdd.MainActivity.<init>(MainActivity.java:19)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1130)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
 
 
 
 
 
J'espère que quelqu'un pourra m'éclairer car se projet compte pour mon obtention du bts et l'épreuve est coefficient 6.
cordialement,
Bécha.
 
 
 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.






×
×
  • Create New...