Home » Android » java – Build error with Realm

java – Build error with Realm

Posted by: admin June 15, 2020 Leave a comment

Questions:

getting below error with building code with Realm

:app:compileDebugJavaWithJavac Note: Processing class
DataBaseQuestion Error:A default public constructor with no argument
must be declared if a custom constructor is declared. Note: Creating
DefaultRealmModule Warning:File for type ‘io.realm.DefaultRealmModule’
created in the last round will not be subject to annotation
processing. Warning:File for type
‘io.realm.DefaultRealmModuleMediator’ created in the last round will
not be subject to annotation processing. 2 warnings Error:Execution
failed for task ‘:app:compileDebugJavaWithJavac’.

Compilation failed; see the compiler error output for details.

Note: A default constructor is already present in my Model or Java Bean Class.

Can anyone please help me how to resolve this?

DataBaseQuestion.java

public class DataBaseQuestion extends RealmObject{

int id;
String Question =null;
String QuestionNo =null;
List<String> optionList=null;
String typeOfQuestion=null;
String Answer = null;
String Explanation = null;

DataBaseQuestion()
{


}
public DataBaseQuestion(int id, String question, String questionNo, List<String> optionList, String typeOfQuestion, String answer, String explanation) {
    this.id = id;
    Question = question;
    QuestionNo = questionNo;
    this.optionList = optionList;
    this.typeOfQuestion = typeOfQuestion;
    Answer = answer;
    Explanation = explanation;
}




public String getQuestion() {
    return Question;
}

public void setQuestion(String question) {
    Question = question;
}

public String getQuestionNo() {
    return QuestionNo;
}

public void setQuestionNo(String questionNo) {
    QuestionNo = questionNo;
}

public List<String> getOptions() {
    return optionList;
}

public void setOptions(List<String> optionList) {
    this.optionList = optionList;
}

public String getTypeOfQuestion() {
    return typeOfQuestion;
}

public void setTypeOfQuestion(String typeOfQuestion) {
    this.typeOfQuestion = typeOfQuestion;
}

public String getAnswer() {
    return Answer;
}

public void setAnswer(String answer) {
    Answer = answer;
}

public String getExplanation() {
    return Explanation;
}

public void setExplanation(String explanation) {
    Explanation = explanation;
}

@Override
public String toString() {
    return "DataBaseQuestion{" +
            "Question='" + Question + '\'' +
            ", QuestionNo='" + QuestionNo + '\'' +
            ", options=" + optionList +
            ", typeOfQuestion='" + typeOfQuestion + '\'' +
            ", Answer='" + Answer + '\'' +
            ", Explanation='" + Explanation + '\'' +
            '}';
}
How to&Answers:

Error:A default public constructor with no argument must be declared

You can add the desired default constructor to the specified class and check back.

Change

DataBaseQuestion() {
}

to

public DataBaseQuestion() {
}

Answer:

you forgot public modifier.

Your program probably tries to reach it outside of package context, which means that it looks only for public constructors. It finds one – the one which requires constructor-args, but doesn’t see package private one. Adding “public” access modifier should solve the problem.

public DataBaseQuestion(){}

Note: You should look at lombok in your spare time, so that you do not manually handle creation of getters, setters, AllArgsContsructors or NoArgsConstructors

Answer:

Another possible cause of this error was defining a package-private object that inherits from RealmObject.

It fails for the same reason as the aforementioned answers: the default constructor is package-private, thus unaccessible to the annotation processor. Declaring the class public fixed it.

Change

class DataBaseQuestion extends RealmObject { }

to

public class DataBaseQuestion extends RealmObject { }

Answer:

Yet another cause of this can be declaring the class twice by mistake in different packages.

If you define your class:

public class DataBaseQuestion extends RealmObject {
    ....
}

And then copy your code into a separate directory

or have it nested in another class as a static class, then this exact same error can be thrown.