Java – About Android Firebase retrieving data (no setter/field errors)

About Android Firebase retrieving data (no setter/field errors)… here is a solution to the problem.

About Android Firebase retrieving data (no setter/field errors)

I

just want to retrieve data from my Firebase database, but I don’t know the right way to get that data. This is my bug in android studio :

  W/ClassMapper: No setter/field for chapterTwo found on class 
  com.junburg.moon.rockbottom.model.Chapter
  W/ClassMapper: No setter/field for chatperOne found on class 
  com.junburg.moon.rockbottom.model.Chapter
  W/ClassMapper: No setter/field for chapterTwo found on class 
  com.junburg.moon.rockbottom.model.Chapter
  W/ClassMapper: No setter/field for chapterOne found on class 
  com.junburg.moon.rockbottom.model.Chapter

This is my data class. This is a Chapter.class model.

public class Chapter {

private String chaterName;
    private String chaterExplain;

public Chapter() {
    }

public Chapter(String chaterName, String chaterExplain) {
        this.chaterName = chaterName;
        this.chaterExplain = chaterExplain;
    }

public String getChaterName() {
        return chaterName;
    }

public void setChaterName(String chaterName) {
        this.chaterName = chaterName;
    }

public String getChaterExplain() {
        return chaterExplain;
    }

public void setChaterExplain(String chaterExplain) {
        this.chaterExplain = chaterExplain;
    }
}

and Study .class model

public class Subject {
  private String subjectName;
  private String subjectExplain;
  private Chapter chapter;

public Subject() {
  }

public Subject(String subjectName, String subjectExplain, Chapter 
   chapter) {
    this.subjectName = subjectName;
    this.subjectExplain = subjectExplain;
    this.chapter = chapter;
  }

public String getSubjectName() {
      return subjectName;
   }

public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
   }

public String getSubjectExplain() {
       return subjectExplain;
   }

public void setSubjectExplain(String subjectExplain) {
       this.subjectExplain = subjectExplain;
   }

public Chapter getChapter() {
       return chapter;
   }

public void setChapter(Chapter chapter) {
        this.chapter = chapter;
   }
}

Here is my addValueEventListener code:

 private void getStudyData() {
      databaseReference.child("study")
     .child("subject")
     .addValueEventListener(new ValueEventListener() {

@Override
      public void onDataChange(DataSnapshot dataSnapshot) {
        subjectList.clear();
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            Subject subject = ds.getValue(Subject.class);
            subjectList.add(subject);
            Log.d(TAG, "onDataChange: " + 
      subject.getChapter().getChaterName()  + 
      subject.getChapter().getChaterExplain());

}
        studyRecyclerAdapter.notifyDataSetChanged();

}

@Override
    public void onCancelled(DatabaseError databaseError) {

}
});

Finally, this is my JSON tree in the Firebase database.

enter image description here

I tried some things but couldn’t fix this error.

Code execution does not return chapter class data. For example:

subject.getChapter().getChaterName() 
subject.getChapter().getChaterExplain()

Thank you so much for helping with this. Thank you for your concern.

Solution

There are several errors in Chapter’s POJO class. First, the error message clearly indicates that the chapterOne and chapterTwo properties are missing. You need it in your POJO.

So your Chapter.java should look like this.

public class Chapter {
    public ChapterOne chapterOne;
    public ChapterTwo chapterTwo;
}

You also need to define the ChapterOne and ChapterTwo classes.

public class ChapterOne extends ChapterDetails {

}

public class ChapterTwo extends ChapterDetails {

}

public class ChapterDetails {
    public String chapterName;
    public String chapterExplain;
}

Notice that you misspelled chapterName and chapterExtreme in Chapter pojo. The current variable names are chaterName and chaterExplain which is wrong. Please also fix the variable name.

Update

But can i get data to change variable type in chapter data of subject
.class? List.. Map.. or.. somethnig else like that. Not make new class
ChapterOne.. ChapterTwo..

For your current implementation, this is not possible. However, I recommend making minor changes to your Firebase database implementation. Let’s modify it with a chapter node.

chapter
    - chapterDetails
        - chapterId : 1
        - chapterName : Something one
        - chapterExplain : Some explanation
    - chapterDetails
        - chapterId : 2
        - chapterName : Something two
        - chapterExplain : Some explanation

If you modify your firebase database structure like above, then you might come up with the following POJO classes.

public class Chapter {
    List<ChapterDetails> chapterDetails;
}

public class ChapterDetails {
    public Long chapterId;
    public String chapterName;
    public String chapterExplain;
}

I’m sorry I asked too much. But the same chapterDetail is not added to
the Firebases. I tried to add a chapterDetail with chapterId 1 and a
chapterDetail with chapterId 2, but it disappears or is replaced.

I just showed a pseudo-implementation. Sorry if the updated answer is misleading. However, use the following methods to set up your data in the Firebase database. This allows you to get your data from Firebase as a list.

Firebase ref = new Firebase("<my-firebase-app>/chapter"):
List<Chapter> chapterList = new ArrayList<Chapter>();
 Now populate your chapterList with the chapters 
addItemsInYourChapterList();
ref.setValue(chapterList); 

Now retrieve the data from the Firebase database as follows.

ref.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot snapshot) {
          System.out.println("There are " + snapshot.getChildrenCount() + " chapters");
          for (DataSnapshot chapterSnapshot: snapshot.getChildren()) {
            Chapter chapter = chapterSnapshot.getValue(Chapter.class);
             Get your chapter details here.
          }
      }
      @Override
      public void onCancelled(FirebaseError firebaseError) {
          System.out.println("The read failed: " + firebaseError.getMessage());
      }
  });

Note that I only considered the chapter node. Modify the code according to your needs.

Related Problems and Solutions