Object is not part of the schema for this Realm - java

As soon as I try to get my object from Realm database, the app crashed and I get this error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.repdev.realtimedelijn/com.repdev.realtimedelijn.activity.MainActivity}:
java.lang.IllegalArgumentException: Haltes is not part of the schema for this Realm
This is my Activity were it happens
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.activity_main);
Context context = this;
View view = this.getWindow().getDecorView();
realm = Realm.getInstance(getRealmConfiguration());
RealmResults<Haltes> haltes = realm
.where(Haltes.class)
.findAll();
HaltesRecyclerViewAdapter haltesRecyclerViewAdapter =
new HaltesRecyclerViewAdapter(this, haltes, true, true);
RealmRecyclerView realmRecyclerView =
(RealmRecyclerView) findViewById(R.id.realm_recycler_view);
realmRecyclerView.setAdapter(haltesRecyclerViewAdapter);
}
and here is the model
Someone an idea how to fix it?
public class Haltes implements RealmModel {
#PrimaryKey
private long id;
private String halteNaam;
private String halteNummer;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getHalteNaam() {
return halteNaam;
}
public void setHalteNaam(String halteNaam) {
this.halteNaam = halteNaam;
}
public String getHalteNummer() {
return halteNummer;
}
public void setHalteNummer(String halteNummer) {
this.halteNummer = halteNummer;
}
}

My problem was solved by declaring apply plugin: 'realm-android' after all other plugins.
App level Gradle
apply plugin: 'android-apt'
apply plugin: 'realm-android'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

had the same problem while using it along side with retrolambda and android-apt.
changing the order of plugins in app level build.gradle file worked for me :
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'
Github issue : https://github.com/realm/realm-java/issues/3783#issuecomment-260578984

In my case I was need to paste kotlin-kapt to app.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt' <<<<<<<<<<the 1st row<<<<<<<<<
apply plugin: 'realm-android' <<<<<the second row<<<<<
I spent 6 hours to solve this problem. And now it works.
And how was written above - realm-android should be added to the end of all plugins!

Are you using the #RealmClass annotation?
If you are using annotations, make sure you have annotation processing enabled in your Android studio settings.

Try this: Android Studio -> Build -> Rebuild & Clean Project

I believe this has to do with adding a new Realm Model class after some models were already added. try un installing the application and run again or migrate your schema.
Does your Haltes class extends RealmObject?
Make it like this:
public class Haltes extends RealmObject
or
#RealmClass
public class Haltes implements RealmModel

You haven't added Realm to your build.gradle file: https://bitbucket.org/repdev/realtimedelijnandroid/src/77c531768dc1250b4d5b5c6c7fd4e6100764177d/build.gradle?at=master&fileviewer=file-view-default
See how here: https://realm.io/docs/java/latest/#installation
Your top level build.gradle file should have this
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "io.realm:realm-gradle-plugin:1.0.1"
}
}
Your app level build.gradle file should have this at the top:
apply plugin: 'realm-android'

For those of you who use a mixture of Kotlin in your codebase, then this problem will be solved by applying kotlin-kapt before realm-android.
That is:
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
Source:
https://github.com/realm/realm-java/issues/5697

I got this exception when using a library project with my app project and realm plugin is only applied to the library project.
When I added realm plugin `apply plugin: 'realm-android' to the app project ,the exception gone.
Make sure realm plugin added to every gradle project that uses realm.

Related

java.lang.ClassNotFoundException: org.postgresql.Driver from jooq code generating task

I am implementing java-spring-boot project ( jdk11, spring boot 2.3.0.RELEASE ) using jooq-hikari-hibernate-postgres for back end.
I have already created the tables using flyway.
I have created a task name "generateJooqRepo" in build.gradle to generate the java code for the repository. The task is running however failing with error message:
Execution failed for task ':generateJooqRepo'.
> java.lang.ClassNotFoundException: org.postgresql.Driver
Any insight on what I am doing wrong?
build.gradle:
import org.jooq.codegen.*
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'org.flywaydb.flyway' version '6.4.4'
id "nu.studer.jooq" version "4.2"
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'application'
apply plugin: 'maven'
apply plugin: 'jacoco'
apply plugin: 'idea'
apply plugin: 'nu.studer.jooq'
configurations {
deployerJars
}
sourceCompatibility = '11.0.1'
targetCompatibility = '11.0.1'
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
//Spring Dependencies
implementation 'org.springframework.boot:spring-boot-starter-actuator:2.3.0.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web:2.3.0.RELEASE'
//postges
jooqRuntime 'org.postgresql:postgresql:42.2.14'
implementation 'org.postgresql:postgresql:42.2.14'
//The gradle-jooq-plugin needs dependencies in a separate configuration.
// It uses the jooqRuntime configuration to detect the needed dependencies, it doesn't use the compile or implementation configuration.
jooqRuntime group: 'org.jooq', name: 'jooq-meta-extensions', version: '3.13.2'
//flyway
compile 'org.flywaydb:flyway-core:6.4.4'
//jooq dependency
implementation 'org.springframework.boot:spring-boot-starter-jooq:2.3.0.RELEASE'
compile 'org.jooq:jooq:3.13.2'
compile 'org.jooq:jooq-meta:3.13.2'
compile 'org.jooq:jooq-codegen:3.13.2'
// This dependency is found on compile classpath of this component.
implementation 'org.apache.commons:commons-lang3:3.9'
}
group = 'com.ringo.tapos.service'
version = '1.0.0-SNAPSHOT'
mainClassName = "com.ringo.tapos.service.Application"
// Use your favourite XML builder to construct the code generation configuration file
// ----------------------------------------------------------------------------------
task generateJooqRepo {
doLast {
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
.configuration('xmlns': 'http://www.jooq.org/xsd/jooq-codegen-3.13.0.xsd') {
jdbc() {
driver('org.postgresql.Driver')
url('jdbc:postgresql://localhost:5432/postgres')
user('postgres')
password('postgres')
schema('tapos')
}
generator() {
database() {
}
generate([:]) {
pojos true
daos true
relations true
deprecated false
records true
//immutablePojos false
}
target() {
packageName('com.ringo.tapos.service.repositories')
directory('src/main/java')
}
}
}
GenerationTool.generate(writer.toString())
}
}
You should add postgres driver dependency to jooqRuntime configuration as well
jooqRuntime 'org.postgresql:postgresql:42.2.14'
UPDATE:
You should add postgres driver to your buildscript classpath if you want to use it in gradle script outside of the tasks provided by nu.studer.jooq plugin
buildscript {
dependencies {
classpath 'org.postgresql:postgresql:42.2.14'
}
}

Cannot create Kotlin object in Java (Android)

I have an Android app written in Java and I am trying to write a new feature in Kotlin. So I added a new Kotlin file and IntelliJ offered to setup the project for Kotlin.
The issue is that when trying to create a Kotlin object in Java, compilation fails with
error: cannot find symbol constructor MyClassKt()
My Kotlin file (MyClass.kt):
val SCREEN = 1;
class MyClass() {
fun hello(view: View) {
}
}
In my app module:
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
In my project module:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Invocation in Java:
new MyClassKt();
Make sure you have these plugin on top in the app gradle file
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
And inside dependencies, should be like
dependencies {
def kotlin_version= "2.2.1"
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
Remove brackets from kotlin class definition.
class MyClassXD {
fun hello(view: View) {
}
}
Probably Java got wild when it saw brackets in classname.
The problem was that Kotlin was creating a MyClassKt for the file (i.e. for accessing the SCREEN variable).
I just needed to use the normal class name and do new MyClass() (no Kt postfix).
Thanks all!

Gradle multiple subproject with different spring boot plugin

I have multi project build structure and spring boot is applied from the parent to all subprojects. Here's the parent build.gradle
buildscript {
repositories {
maven {
url 'http://someurl.com/repository/MavenRepositoryGroup/'
}
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE"
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
..
..
..
}
}
Among multiple subprojects, I have a requirement to upgrade just one subproject to spring boot 2 version. How to do it?
It's pretty easy, just declare plugins in subprojects directly, where they are used. BTW, consider using new plugins DSL instead of apply. Take a look at a demo project I created for you. Here are the most interesting parts:
settings.gradle:
rootProject.name = "so53381565"
enableFeaturePreview("IMPROVED_POM_SUPPORT")
include(":a")
include(":b")
include(":c")
build.gradle:
subprojects {
apply plugin: "java"
repositories {
jcenter()
}
}
wrapper {
gradleVersion = "4.10.2"
distributionType = Wrapper.DistributionType.ALL
}
a/build.gradle:
plugins {
id "org.springframework.boot" version "1.5.8.RELEASE"
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter"
}
b/build.gradle:
plugins {
id "org.springframework.boot" version "1.5.17.RELEASE"
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter"
}
c/build.gradle:
plugins {
id "org.springframework.boot" version "2.0.6.RELEASE"
}
dependencies {
implementation "org.springframework.boot:spring-boot-dependencies:2.0.6.RELEASE"
implementation "org.springframework.boot:spring-boot-starter"
}
App.java:
#SpringBootApplication
public class App implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
#Override
public void run(String... args) throws Exception {
System.out.println(SpringVersion.getVersion());
}
}
App.java is the same in all three subprojects. Executing ./gradlew clean :a:bootRun :b:bootRun :c:bootRun will output something like:
…
4.3.12.RELEASE
…
4.3.20.RELEASE
…
5.0.10.RELEASE
…
As you see, you're using three different Springs in three different subprojects.
EDIT
First of all, you can still use apply, just put them in subprojects.
Second, you can use plugins with your own repositories and dependencies, even with those not published in Gradle's Plugin Repository by adding a pluginManagement block in settings.gradle (this is Kotlin DSL from my project):
pluginManagement {
repositories {
gradlePluginPortal()
add(jcenter())
add(maven("http://my.jfrog.io"))
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "by.dev.madhead.some-plugin") {
useModule("by.dev.madhead:some-gradle-plugin:${requested.version}")
}
}
}
}
Now I am able to use by.dev.madhead.some-plugin in plugins DSL across the project:
plugins {
id("by.dev.madhead.some-plugin").version("42")
}
And it will be substituted by by.dev.madhead:some-gradle-plugin:42

Passing JVM args from build.gradle in Spring Boot 2.0.3

I'm having some trouble trying to pass JVM arguments to a Spring Boot application from build.gradle file.
My build.gradle looks like this:
buildscript ...
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
...
def devConfigFolder = "/abc"
applicationDefaultJvmArgs = ["-DconfigFolder=$devConfigFolder"]
dependencies {
...
}
And the class where I try to use the placeholder:
#Component
public class PClass {
private static final String CONF = "configFolder";
#Value("${" + CONF+ "}")
private String configFolder;
}
And this is the exception I'm receiving:
IllegalArgumentException: Could not resolve placeholder 'configFolder' in value "${configFolder}"
I've tried with bootRun{jvmArgs}, but it doesn't work.
You can set these values in application.property /yml file and use that property key in #value annotation.
application-prod.properties
configFolder="somefolder/path"
These property files can be set/modified in runtime.
You can take a look at this (5.1) section: https://www.baeldung.com/properties-with-spring
Is that fine for you?? If not please let me know.

"Object" is not part of the schema for this Realm

When I call the realm.where(MessageEventModel::class.java).findAll()
it throws excepiton:
this is error
java.lang.IllegalArgumentException: MessageEventModel is not part of the schema for this Realm
at io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:172)
at io.realm.internal.modules.CompositeMediator.getTableName(CompositeMediator.java:90)
this is my Application class
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
val realmConfiguration = RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.name("my_db")
.build()
Realm.setDefaultConfiguration(realmConfiguration)
}
}
this is my Realm Model
class MessageEventModel : RealmObject{
constructor()
var message = ""
constructor(message: String) : this(){
this.message = message
}
}
And here is where I'm trying to retrieve models
class AwesomeChatFragment : Fragment() {
private val realm: Realm by lazy { Realm.getDefaultInstance() }
private var notifications: RealmResults<MessageEventModel>? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater?.inflate(R.layout.activity_awesome_chat, container, false)
notifications = realm.where(MessageEventModel::class.java).findAll()
return view
}
}
gradle configuration:
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
buildscript {
ext.kotlin_version = '1.1.1'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.google.gms:google-services:3.0.0'
classpath "io.realm:realm-gradle-plugin:3.0.0"
}
}
I tried everything what I could found on stack:
clean build, rebuild project, enable annotation processor, reinstaling apk, invalidate caches / restart
The issue where in gradle file.The problem where just an ordering rules of applying plugins, thanks to the #EpicPandaForce's comment, the problem has been solved, I'm writing answer, for helping others, if they miss the commented answer from #EpicPandaForce
I changed the ordering of
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
to
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
That's all, now everything works fine

Categories