I was creating an android project to test db4o and ran into some trouble, I compiled the db4o-8.0.184.15484-all-java5.jar jar file in build.gradle under the libs folder. Within my db4oHelper class I have the following:
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();
}
}
I have another class called DB4OProvider which contains:
/**
* 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);
}
}
I also have a very simple Student class that just takes in the name, age and Registration number in the constructor.
But when I try to submit the data, I get a very lengthy error message saying:
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)
I dont understand why it is a null pointer exception or how to fix it, any help would be greatly appreciated, Thanks
Are you sure that you get all the logging and oc is not null (catch part of code)?