在android中设置db4o

时间:2015-12-02 07:14:29

标签: java android database db4o

我正在创建一个测试db4o的android项目并遇到了一些麻烦,我在libs文件夹下的db4o-8.0.184.15484-all-java5.jar编译了build.gradle jar文件。在我的db4oHelper类中,我有以下内容:

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);
}
}

我还有一个非常简单的Student类,它只接受构造函数中的名称,年龄和注册号。

但是当我尝试提交数据时,我收到一条非常冗长的错误消息:

 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) 

我不明白为什么它是一个空指针异常或如何解决它,任何帮助将不胜感激,谢谢

1 个答案:

答案 0 :(得分:0)

您确定获得所有日志记录且oc不是null( catch 代码的一部分)?