5

When I start the activity, it is showing the error:

Class com.rcpl.agni.Artist does not define a no-argument constructor.If you are using ProGuard, make sure these constructors are not stripped.

protected void onStart() {
        super.onStart();
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //artistList.clear();
                for(DataSnapshot artistSnapshot : dataSnapshot.getChildren()){
                    Artist artist = artistSnapshot.getValue(Artist.class);
                    Toast.makeText(MainActivity.this, artist.getArtistName(), Toast.LENGTH_SHORT).show();

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

Artist.java

package com.rcpl.agni;

/**
 * Created by Dell-pc on 23-01-2018.
 */

public class Artist {
    String artistId;
    String artistName;
    String artistGenre;

    public Artist(String artistId, String artistName, String artistGenre) {
        this.artistId = artistId;
        this.artistName = artistName;
        this.artistGenre = artistGenre;
    }

    public String getArtistId() {
        return artistId;
    }

    public String getArtistName() {
        return artistName;
    }

    public String getArtistGenre() {
        return artistGenre;
    }
}

3 Answers 3

9

To solve this, you just need to add the public no-argument constructor to your Artist class.

public Artist() {}

Remember, when the Firebase Realtime Database SDK deserializes objects coming from the database, it requires that any objects to a have a public no-argument constructor. This is needed to instantiate that particular object. All the fields within the object are set by using public setter methods or direct access to public members.

If you don't use a public no-arg constructor, the SDK doesn't know how to create an instance of the class. Most serialization libraries will have the same requirement.

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

2 Comments

@RosárioPereiraFernandes Indeed and this is because we almost answered in the same time with the only one solution that solves OP's problem. Firebase requires this constructor in order to use it's correct, but why? Everytime is good practice not only to solve the problem so to explain to the OP's a little more about that process.
I Agree. That's why I removed my comment.
3

I believe the error is pretty self-explanatory. You just need to add an empty constructor to your Artist class:

package com.rcpl.agni;

/**
 * Created by Dell-pc on 23-01-2018.
 */

public class Artist {
    String artistId;
    String artistName;
    String artistGenre;

    public Artist(){}

    public Artist(String artistId, String artistName, String artistGenre) {
        this.artistId = artistId;
        this.artistName = artistName;
        this.artistGenre = artistGenre;
    }

    public String getArtistId() {
        return artistId;
    }

    public String getArtistName() {
        return artistName;
    }

    public String getArtistGenre() {
        return artistGenre;
    }
}

Firebase requires this constructor in order to use it when you call artistSnapshot.getValue(Artist.class);

Comments

0

enter image description here

add the non-argument in the Data class

enter image description here

Run the app with a clear logcat:

enter image description here

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.