これは私の DBhelper クラスです。データベースを assest フォルダから現在の DB パスにコピーしたいのですが、CopyDatabase 関数の InputStream でエラーが発生しています。助けてください。
package com.example.dbtest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
public class DBhelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.example.dbtest/databases/";
private static String DB_NAME = "photodb";
private SQLiteDatabase myDataBase;
private final Context myContext;
File dbFile;
File dbFilepath;
public DBhelper(Context context) {
super(context, DB_NAME, null, 1);
String state=Environment.getExternalStorageState();
System.out.println("My Storage State:-"+state);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
System.out.println("Ufffff");
String path = dbFile.getAbsolutePath();
System.out.println("My database Path:--"+path);
}
if(!dbExist)
{
dbFile= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Dbtest");
boolean flag=dbFile.mkdir();
dbFilepath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Dbtest"+" /"+"photodb");
dbFilepath.createNewFile();
System.out.println("Ufffff naaa..File hoeche?..."+flag);
try {
copyDataBase(myContext);
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public boolean checkDataBase()
{
dbFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Dbtest"+" /"+"photodb");
return dbFile.exists();
}
private void copyDataBase(Context cont) throws IOException{
InputStream myInput = cont.getAssets().open(DB_NAME);
String outFileName = dbFilepath.getAbsolutePath();
OutputStream myOutput = new FileOutputStream(outFileName);
//OutputStream myOutput1 = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = dbFile.getAbsolutePath();
System.out.println("MY DATABASE PATH:----"+myPath);
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void add(String imagepath,String description,String location)
{
String sql = "insert into photodetails values(null,'hello','bolo','aache');";
myDataBase.execSQL(sql);
}
}
私のMainActivityは
public class MainActivity extends Activity {
DBhelper mydb;
Context cont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBhelper(cont);
try {
mydb.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mydb.openDataBase();
mydb.add("helloworld", "Hoeche", "MGroad");
// mydb.openDataBase();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
私のLogcatエラーは
09-03 11:48:40.342: E/AndroidRuntime(27139): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dbtest/com.example.dbtest.MainActivity}: java.lang.NullPointerException
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.access$700(ActivityThread.java:140)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.os.Looper.loop(Looper.java:137)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.main(ActivityThread.java:4921)
09-03 11:48:40.342: E/AndroidRuntime(27139): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 11:48:40.342: E/AndroidRuntime(27139): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
09-03 11:48:40.342: E/AndroidRuntime(27139): at dalvik.system.NativeStart.main(Native Method)
09-03 11:48:40.342: E/AndroidRuntime(27139): Caused by: java.lang.NullPointerException
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.example.dbtest.DBhelper.copyDataBase(DBhelper.java:98)
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.example.dbtest.DBhelper.createDataBase(DBhelper.java:77)
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.example.dbtest.MainActivity.onCreate(MainActivity.java:25)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.Activity.performCreate(Activity.java:5206)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
09-03 11:48:40.342: E/AndroidRuntime(27139): ... 11 more
アップデート
行を変えて書いても
AssetManager myassest = cont.getAssets();
InputStream myInput = myassest.open("photodb");
cont.getAssests(); でエラーが発生しました。その言っているNULLポインタ例外