Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build error with Realm

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 + '\'' +
            '}';
}
like image 363
Bat Avatar asked May 22 '16 11:05

Bat


3 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() {
}
like image 159
Naman Avatar answered Nov 12 '22 10:11

Naman


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

like image 30
Andrii Plotnikov Avatar answered Nov 12 '22 10:11

Andrii Plotnikov


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 { }

like image 41
verybadalloc Avatar answered Nov 12 '22 12:11

verybadalloc