jelmood jasser jelmood jasser - 6 months ago 157
Android Question

No default RealmConfiguration was found. Call setDefaultConfiguration()

I am trying to learn how to use Realm by creating simple testing app, but I am getting this error always. I am using Realm 1.0.0.
The error in the logcat:

java.lang.NullPointerException: No default RealmConfiguration was found.
Call setDefaultConfiguration()


This is the code in the Mainactivity.

public class MainActivity extends AppCompatActivity {
EditText name;
EditText age;
TextView result;
Realm myRealm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name= (EditText) findViewById(R.id.editText);
age=(EditText)findViewById(R.id.editText2);
result= (TextView) findViewById(R.id.textView);
myRealm= Realm.getDefaultInstance();

}
public void store(){
myRealm.beginTransaction();
User user=myRealm.createObject(User.class);
user.setName(name.getText().toString());
user.setAge(Integer.parseInt(age.getText().toString()));
myRealm.commitTransaction();
}

public void load(View view) {
RealmResults<User>results=myRealm.where(User.class).findAll();
StringBuffer stingBuffer=new StringBuffer();
for(User user:results){
stingBuffer.append(user.getName()+" - "+user.getAge()+"\n");
}
result.setText(stingBuffer.toString());
}

Answer

add RealmCOnfiguration in you MyApplication file

// Setup Realm in your Application

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
        Realm.setDefaultConfiguration(realmConfiguration);
    }
}
Comments