0

I tried to store data in firebase database but the following errors occurs:

Process: com.example.zar.shoppinglistplusfirebase, PID: 2379 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source) at com.google.android.gms.internal.zzbqi.zzax(Unknown Source) at com.google.android.gms.internal.zzbqi.zzaw(Unknown Source) at com.google.firebase.database.DatabaseReference.zza(Unknown Source) at com.google.firebase.database.DatabaseReference.setValue(Unknown Source) at com.example.zar.shoppinglistplusfirebase.ui.activeLists.AddListsFragment.addShoppingList(AddListsFragment.java:115) at com.example.zar.shoppinglistplusfirebase.ui.activeLists.AddListsFragment$1.onEditorAction(AddListsFragment.java:74)

Pojo ShoppingList

package com.example.zar.shoppinglistplusfirebase.model;

import com.example.zar.shoppinglistplusfirebase.Utils.Constants;
import com.google.firebase.database.ServerValue;
import java.util.HashMap;



public class ShoppingList {
private String listName;
private String owner;
private HashMap<String, Object> timestampLastChanged;

/**
 * Required public constructor
 */
public ShoppingList() {
}

/**
 * Use this constructor to create new ShoppingLists.
 * Takes shopping list listName and owner. Set's the last
 * changed time to what is stored in ServerValue.TIMESTAMP
 *
 * @param listName
 * @param owner
 *
 */
public ShoppingList(String listName, String owner,HashMap<String,Object> timampLastChangedObj) {
    this.listName = listName;
    this.owner = owner;
    HashMap<String, Object> timestampLastChangedObj = new HashMap<String, Object>();
    timestampLastChangedObj.put(Constants.FIREBASE_PROPERTY_TIMESTAMP, ServerValue.TIMESTAMP);
    this.timestampLastChanged = timestampLastChangedObj;

}

public String getListName() {
    return listName;
}

public String getOwner() {
    return owner;
}

public HashMap<String, Object> getTimestampLastChanged() {
    return timestampLastChanged;
}



public long getTimestampLastChangedLong() {

    return (long) timestampLastChanged.get(Constants.FIREBASE_PROPERTY_TIMESTAMP);
}
}

Data Writing to firebase

  FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
        DatabaseReference databaseReference=firebaseDatabase.getReferenceFromUrl(Constants.FIREBASE_ACTIVE_LIST_URL);
        DatabaseReference ref=databaseReference.push();

        HashMap<String,Object> timeStampCreated=new HashMap<>();
        timeStampCreated.put(Constants.FIREBASE_PROPERTY_TIMESTAMP, ServerValue.TIMESTAMP);

        ShoppingList newShoppingList = new ShoppingList(userEnteredName, owner,timeStampCreated);
        ref.setValue(newShoppingList);
        AddListsFragment.this.getDialog().cancel();
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Zar Saeed
  • 100
  • 2
  • 13

1 Answers1

0

Firebase sees your getTimestampLastChangedLong() getter and looks for a corresponding property that doesn't exist. Use the @Exclude annotation for your getTimestampLastChangedLong() method, and Firebase will ignore it. Explained in more detail here: https://stackoverflow.com/a/36659507/4816918

Community
  • 1
  • 1
Jeff
  • 2,425
  • 1
  • 18
  • 43