0
我正在創建一個android項目來測試db4o並且遇到了一些麻煩,我編譯了db4o-8.0.184.15484-all-java5.jar
jar文件,它位於libs文件夾下的build.gradle
。在我的db4oHelper類我有以下幾點:在android中設置db4o
import java.io.IOException;
import android.content.Context;
import android.util.Log;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;
public class Db4oHelper {
private static ObjectContainer oc = null;
private Context context;
/**
* @param ctx
*/
public Db4oHelper(Context ctx) {
context = ctx;
}
/**
* Create, open and close the database
*/
public ObjectContainer db() {
try {
if (oc == null || oc.ext().isClosed()) {
oc = Db4oEmbedded.openFile(dbConfig(), db4oDBFullPath(context));
}
return oc;
} catch (Exception ie) {
Log.e(Db4oHelper.class.getName(), ie.toString());
return null;
}
}
/**
* Configure the behavior of the database
*/
private EmbeddedConfiguration dbConfig() throws IOException {
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common().objectClass(Student.class).objectField("name")
.indexed(true);
configuration.common().objectClass(Student.class).cascadeOnUpdate(true);
configuration.common().objectClass(Student.class)
.cascadeOnActivate(true);
return configuration;
}
/**
* Returns the path for the database location
*/
private String db4oDBFullPath(Context ctx) {
return ctx.getDir("data", 0) + "/" + "myDatabase.db4o";
}
/**
* Closes the database
*/
public void close() {
if (oc != null)
oc.close();
}
}
我還有一個叫DB4OProvider類,其中包含:
/**
* Created by manish on 2015-12-01.
*/
import java.util.List;
import android.content.Context;
public class DB4OProvider extends Db4oHelper {
private static DB4OProvider provider = null;
//configure Db4oHelper by Passing Context.
public DB4OProvider(Context ctx) {
super(ctx);
}
public static DB4OProvider getInstance(Context ctx) {
if (provider == null)
provider = new DB4OProvider(ctx);
return provider;
}
//This method is used to store the object into the database.
public void store(Student exercise) {
db().store(exercise);
}
//This method is used to delete the object into the database.
public void delete(Student exercise) {
db().delete(exercise);
}
//This method is used to retrive all object from database.
public List<Student> findAll() {
return db().query(Student.class);
}
//This method is used to retrive matched object from database.
public List<Student> getRecord(Student s) {
return db().queryByExample(s);
}
}
我也有一個非常簡單的學生類,只需要在姓名,年齡和登記編號在構造函數中。
但是,當我嘗試提交數據,我得到一個非常漫長的錯誤消息說:
Caused by: java.lang.NullPointerException: Attempt to invoke interface
method 'void com.db4o.ObjectContainer.store(java.lang.Object)' on a null object reference
at com.example.manish.dbtest.DB4OProvider.store(DB4OProvider.java:26)
at com.example.manish.dbtest.MainActivity.onSubmit(MainActivity.java:47)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我不明白爲什麼它是一個空指針異常或如何解決它,任何幫助將不勝讚賞,謝謝
可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -它) – Zoe