1

I am attempting to learn how to implement Firebase into an existing project that previously used a SQL db. I have the write code working correctly, but my read is throwing the DatabaseException error below.

com.google.firebase.database.DatabaseException: Found a conflicting setters with name: setWallpaper (conflicts with setWallpaper defined on android.content.ContextWrapper)

I have done a good bit of research and can only find references to this error when writing to the database, not reading from it, so I am very much confused as to what I might have done in my code to cause this error to happen. The method code is below.

protected void lastFive() {
    final DatabaseReference gameRef = database.getReference("Games");
    final List<User> gamesList = new ArrayList<>();
    gameRef.orderByChild("Date").limitToLast(5).addValueEventListener (new ValueEventListener ( ) {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            gamesList.clear ();
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren ()){
                User user = postSnapshot.getValue(User.class);
                gamesList.add (user);
                System.out.println(user.getName () + " : " + user.getScore ());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}

The line in the method the error appears to be referencing is:

User user = postSnapshot.getValue(User.class);

I have only been working in Android for a couple months off and on, and Firebase less than half that, so it is possible there is some glaring error in this I simply do not see. FWIW, this method is intended to display the username and score for the last 5 games played on the app. I am a bit concerned what the output will look like, which I know is outside the scope of this question, but I am unable to even work on that until I can read the data. Any help or suggestion is greatly appreciated.

User code being added as requested.

public class User extends Application {

    //private variable
    private static User globalVar;

    //variables
    String _id;
    String _name;
    int _score;
    String _date;
    String _email;

    // Empty constructor
    public User(){}

    // constructor
    public User(String id, String name, int score, String date){
        this._id = id;
        this._name = name;
        this._score = score;
        this._date = date;
    }

    public User(String id, int score){
        this._id = id;
        this._score = score;
    }

    public User(String id){
        this._id = id;
    }

    public User(int score, String date){
        this._score = score;
    }

    // getting ID
    public String getID(){
        return this._id;
    }

    // setting id
    public void setID(String id){
        this._id = id;
    }

    // getting name
    public String getName(){
        return this._name;
    }

    // setting name
    public void setName(String name){
        this._name = name;
    }

    // getting scores
    public int getScore(){ return this._score; }

    // setting scores
    public void setScore(int score){ this._score = score; }

    // getting name
    public String getDate(){
        return this._date;
    }

    // setting name
    public void setDate(String date){
        this._date = date;
    }

    // getting email
    public String getEmail(){
        return this._email;
    }

    // setting email
    public void setEmail(String email){
        this._email = email;
    }

    public static synchronized User getGlobalVar(){
        if(globalVar == null){
            globalVar = new User();
        }
        return globalVar;
    }
}
3
  • 2
    Please show the source code for the User class. Commented Sep 5, 2017 at 4:19
  • 1
    Please add model class for user i guess there is some issue there. Are you putting values in fire base DB before retrieving it. Commented Sep 5, 2017 at 4:26
  • User code has been added and values are added to FB prior to this code being run. Ideally, I would like it displayed in a pop up window when all is said and done and display a message "No games played" when the DB is empty, but that is once I understand how to retrieve the data successfully. Commented Sep 5, 2017 at 15:29

1 Answer 1

1

With the exception posted I can conclude that you don't have the getters/setters for the fields in your User.class. Ideally your User class should be like:

   public class User{
   private String name;

   public String getName(){
   return name;
   }

   public void setName(String name){
   this.name = name;
   }
}

Please verify if you have class structure in this way.

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

6 Comments

I added the user class structure to main post. I'm sorry I did not think to do so from the start.
What is the Application class?? is it Android's Application class?
I have the User class set as the Application class in order to pass a global variable when I am sending data earlier in the code.
DON'T DO THAT... THAT IS BIGGEST FLAW... take the things you want in the other class
You were right, extending on Application was the issue.
|

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.