Reputation: 259
So I have a user class. And the user class has a arraylist of users called followers. When i try to upload a user object (with a few users in the followers arraylist for this user object) Firebase isn't able to do it and my program crashes...I can't figure how to store and retrieve my own objects that have in them other customized objects.
So the main error i get is:
unable to start activity ComponentInfo{com.daprlabs.swipedeck/com.daprlabs.swipedeck.ActivityCenter}: com.firebase.client.FirebaseException: Failed to parse to snapshot
Here is my java code.
EDITED: adding @exclude, but still doesn't work...
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRef = new Firebase("Fire base reference...");
dummyUser();
}
private void dummyUser()
{
//Andie <- Chris <- Qamar
mainDummyUser = new User();
mainDummyUser.setName("Chris");
mainDummyUser.setLocation("Deutchland");
mainDummyUser.setDummyImage(R.drawable.chris);
//Second Dummy
User secondDummy = new User();
secondDummy.setName("Andie");
secondDummy.setLocation("Germany");
secondDummy.setDummyImage(R.drawable.andie);
secondDummy.addtoFollowers(mainDummyUser);
mainDummyUser.addtoFollowers(secondDummy);
//Add users to Firebase
mRef.child("users").child("Dummy User 1").setValue(mainDummyUser);
}
Here is my user class, pretty simple.
public class User
{
private String userName;
private int userID;
private String userEmail;
private String userProfilePicture;
private ArrayList<CityFolders> userFolders = new ArrayList<>();
//Constructor
public User()
{
setDummyUserID();
}
public void addtoFollowers(User user)
{
userFollowers.add(user);
}
public void setLocation(String location)
{
this.location = location;
}
public void setDummyImage(int image)
{
this.dummyImage = image;
}
public void setDummyUserID()
{
Random randomNumberGenerator = new Random();
userID = randomNumberGenerator.nextInt(1000000);
}
//Getter
public String getUserName()
{
return userName;
}
public String getLocation()
{
return location;
}
public int getUserID()
{
return userID;
}
@Exclude
public ArrayList<UserFinal> getUserFollowers()
{
return userFollowers;
}
//Goes on...
}
Here is my stack trace:
08-24 19:07:26.549 23384-23384/com.daprlabs.swipedeck E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.daprlabs.swipedeck, PID: 23384
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.daprlabs.swipedeck/com.daprlabs.swipedeck.ActivityCenter}: com.firebase.client.FirebaseException: Failed to parse to snapshot
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: com.firebase.client.FirebaseException: Failed to parse to snapshot
at com.firebase.client.Firebase.setValueInternal(Firebase.java:357)
at com.firebase.client.Firebase.setValue(Firebase.java:248)
at com.daprlabs.swipedeck.ActivityCenter.dummyUser(ActivityCenter.java:165)
at com.daprlabs.swipedeck.ActivityCenter.onCreate(ActivityCenter.java:122)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.IllegalArgumentException: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowing"]->java.util.ArrayList[0]->com.daprlabs.swipedeck.Globals.User["userFollowers"]->java.util.Arra
08-24 19:07:26.577 23384-23394/com.daprlabs.swipedeck W/art: Suspending all threads took: 32.173ms
Upvotes: 1
Views: 4171
Reputation: 11
How about to use not User but UserID ? Like
private void dummyUser()
{
mainDummyUser = new User();
mainDummyUser.setName("Chris");
mainDummyUser.setLocation("Deutchland");
User secondDummy = new User();
secondDummy.setName("Andie");
secondDummy.setLocation("Germany");
secondDummy.addFollowerUserIDs(mainDummyUser.getUserID());
mainDummyUser.addFollowerUserIDs(secondDummy.getUserID());
//Add users to Firebase
mRef.child("users").child(mainDummyUser.getUserID()).setValue(mainDummyUser);
mRef.child("users").child(secondDummy.getUserID()).setValue(secondDummy);
}
and
public class User {
private String userName;
private String location;
private String userID;
private ArrayList<String> FollowerUserIDs = new ArrayList<>();
//Constructor
@SuppressWarnings("unused")
public User()
{
setDummyUserID();
}
public void addFollowerUserIDs(String userID)
{
FollowerUserIDs.add(userID);
}
public void setLocation(String location)
{
this.location = location;
}
public void setDummyImage(int image)
{
// this.dummyImage = image;
}
public void setDummyUserID()
{
Random randomNumberGenerator = new Random();
userID = String.valueOf(randomNumberGenerator.nextInt(1000000));
}
public void setUserID()
{
Random randomNumberGenerator = new Random();
userID = String.valueOf(randomNumberGenerator.nextInt(1000000));
}
//Getter
public String getUserName()
{
return userName;
}
public void setName(String userName)
{
this.userName = userName;
}
public String getLocation()
{
return location;
}
public String getUserID()
{
return userID;
}
public ArrayList<String> getFollowerUserIDs()
{
return FollowerUserIDs;
}
}
Also, your sample code is
public class User
{
private String userName;
private int userID;
private String userEmail;
private String userProfilePicture;
private ArrayList<CityFolders> userFolders = new ArrayList<>();.
but it should be
public class User
{
private String userName;
private int userID;
private String userEmail;
private String userProfilePicture;
private ArrayList<User> userFollowers = new ArrayList<>();
Upvotes: 1
Reputation: 1459
Try adding @Exclude
to the getter method of the userFollowing
property. This should break the infinite loop issue.
@Exclude
public User getUserFollowing()
This works if you always update the user in this way (having a user and his followers). It might be an issue if you want to update a user that is a follower directly.
Upvotes: 0