5

I am new to Firebase Database, I just want to Save the array of data in Firebase database. when I am trying to save it shows error. Below I mentioned the POJO Class and Logcat Error while save the data.

POJO Class Coding

@IgnoreExtraProperties
public class StudentReviews {

public String student_name;
public String student_class;
public String student_image;
public String student_section;

public ArrayList<Reviews> reviewsList = new ArrayList<>();

public void StudentReviews() {}

public class Reviews {
        public String subject;
        public String marks_taken;
        public String total_marks;
        public String teacher;
    }
}

Saving Data

StudentReviews studentReviews = new StudentReviews();
studentReviews.student_name = et_studentName.getText().toString();
studentReviews.student_class = et_student_class.getText().toString();
studentReviews.student_image = "";
studentReviews.student_section = sp_section.getSelectedItem().toString();
studentReviews.reviewsList = reviewsArray;

dbUploadReview.push().setValue(studentReviews); //165th Line in Logcat Error

Logcat Error

com.google.firebase.database.DatabaseException: Invalid key: this$0. Keys must not contain '/', '.', '#', '$', '[', or ']'
at com.google.android.gms.internal.zzbtf.zzjq(Unknown Source)   
at com.google.android.gms.internal.zzbtf.zzay(Unknown Source)
at com.google.android.gms.internal.zzbtf.zzay(Unknown Source)
at com.google.android.gms.internal.zzbtf.zzay(Unknown Source)  
at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
at com.trending.trendingadmin.fragment.WriteReview.uploadReviews(WriteReview.java:165)

When I remove this public ArrayList<Reviews> reviewsList = new ArrayList<>(); reviewsList in POJO Class data saved in Firebsae database perfectly.

Anyone know help me to solve this issue.

Update

I am Solved this problem by Two Ways. Suggestion given by OlegOsipenko

1) Make the inner class as static

2) Moving the inner class to another file.

Now its work well and good as i expected.

15
  • What is line 165 of WriteReview.java? Commented Jun 19, 2017 at 5:31
  • Also please provide a minimal reproducible example. Commented Jun 19, 2017 at 5:31
  • @Yugesh your data must not contain '/', '.', '#', '$', '[', or ']'.It means your data subject or teacher or marks or dbUploadReview may have that symbols.try once with only characters or numbers to find occuring same issue.. Commented Jun 19, 2017 at 5:32
  • 1
    And by the way, change the type of your reviewsList variable to be interface List type instead of specific implementation. And why do you initialise it to the ArrayList instance? Anyway you'll assign to this variable another List object, and you're just performing unnecessary allocations, loading your GC with extra work Commented Jun 19, 2017 at 7:49
  • 1
    @Yugesh actually your code is almost ok. Try to make Reviews class static and run Commented Jun 19, 2017 at 8:10

2 Answers 2

2

As your Logcat error says Keys must not contain '/', '.', '#', '$', '[', or ']', which means that each one of this symbols is not allowed as a key in Firebase database.

In order to write data objects to your Firebase database, in stead of using a List, i suggest you to use a Map. You need also to make this change in your pojo class. To save data, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference namesRef = rootRef.child("names");
Map<String, Object> map = new HashMap<>();
map.put("nameId1", "John");
map.put("nameId2", "Tim");
map.put("nameId3", "Sam");
//and os on
namesRef.updateChildren(map);

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

No, See my Question Update.
1

The .setValue() method needs a List rather than an Array. You can use List of object.

Firebase ref = new Firebase("<my-firebase-app>/names"):
String[] names = {"John","Tim","Sam","Ben"};
List nameList = new ArrayList<String>(Arrays.asList(names));
// Now set value with new nameList
ref.setValue(nameList);

Read the Firebase docs for more information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.