I have a class which extends SugarRecord
, here is it:
class Advertiser : SugarRecord<Advertiser> {
var number: String? = null
var name: String? = null
constructor() {}
constructor(number: String, name: String) {
this.number = number
this.name = name
}
}
I have sugar metadata in manifest:
<meta-data android:name="DATABASE" android:value="sugar_example.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
I'm trying to create new instance of Advertiser
and save it in MainActivity.kt
:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val advertiser = Advertiser("123123", "test advertiser")
advertiser.save()
}
}
but I'm getting exception, here is stack trace:
FATAL EXCEPTION: main Process: ge.ketili.team.blocksms, PID: 22305 java.lang.RuntimeException: Unable to start activity ComponentInfo{ge.ketili.team.blocksms/ge.ketili.team.blocksms.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.orm.Database com.orm.SugarApp.getDatabase()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2706) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6205) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.orm.Database com.orm.SugarApp.getDatabase()' on a null object reference at com.orm.SugarRecord.save(SugarRecord.java:45) at ge.ketili.team.blocksms.MainActivity.onCreate(MainActivity.kt:15) at android.app.Activity.performCreate(Activity.java:6864) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
Your application should be SugarApp
in AndroidManifest.xml
.
<application android:name="com.orm.SugarApp"
...
</application>