2015-12-02 51 views
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) 

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

+0

可能重复[什么是NullPointerException,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -它) – Zoe

回答

0

你确定你得到的所有日志和oc不为空(catch部分代码)?