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 + '\'' +
'}';
}
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() {
}
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
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 { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With