JsonMappingException: No suitable constructor found

brainbreaker

I am implementing a firebase example as given in their documentations. I am facing this error:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.XYZ.$BlogPost]: can not instantiate from JSON object (need to add/enable type information?)

Here is my code:

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        Firebase.setAndroidContext(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Get a reference to our posts
        Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
        // Attach an listener to read the data at our posts reference
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");
                for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                    BlogPost post = postSnapshot.getValue(BlogPost.class);
                    System.out.println(post.getAuthor() + " - " + post.getTitle());
                }
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });
    }
public class BlogPost {
        private String author;
        private String title;
        public BlogPost() {
            // empty default constructor, necessary for Firebase to be able to deserialize blog posts
        }
        public String getAuthor() {
            return author;
        }
        public String getTitle() {
            return title;
        }
    }
}

I have gone through many questions on the same thing saying to include empty constructor necessary to deserialize the JSON. I have included that but still I am not able to resolve the issue. This is the JSON I am trying to deserialize:

{  
   "-JRHTHaIs-jNPLXOQivY":{  
      "author":"gracehop",
      "title":"Announcing COBOL, a New Programming Language"
   },
   "-JRHTHaKuITFIhnj02kE":{  
      "author":"alanisawesome",
      "title":"The Turing Machine"
   }
}

I don't know what I am missing in my code. Any help regrading this is appreciated.

Frank van Puffelen

I'm guessing your BlogPost class is an inner class of your activity. In that case Java adds a hidden field to each object that refers to the containing object. And this implicit field is of course not present in your JSON data.

To solve this, you should either keep the BlogPost class in a separate file (so that it's not an inner class) or mark it as a static class (which removes the implicit field):

public static class BlogPost {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JsonMappingException: No suitable constructor found for type

From Dev

JsonMappingException: No suitable constructor found for type -- for an external object

From Java

JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

From Dev

JsonMappingException: No suitable constructor found for type [simple type, class car.Car$Parts]

From Dev

Deserializing Guava's Table with Jackson throws "JsonMappingException: No suitable constructor found for type"

From Dev

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of : poja class no suitable constructor found

From Dev

Deserializing Guava's Table with Jackson throws "JsonMappingException: No suitable constructor found for type"

From Dev

No Suitable Constructor Found (Java)

From Dev

Java no suitable constructor found

From Dev

No suitable constructor found for JsonObjectRequest

From Dev

No suitable Constructor Found

From Dev

No suitable constructor found for java

From Dev

No suitable constructor found for type GeoJsonPoint

From Dev

CsvParser "No suitable constructor found for type"

From Dev

No Suitable constructor found for File(file)

From Dev

No Suitable Constructor Found for Student (no arguments)

From Dev

No Suitable constructor found for File(file)

From Dev

No suitable constructor found for type GeoJsonPoint

From Dev

Chrome driver with Selenium : "no suitable constructor found for RemoteWebDriver"

From Dev

Suitable constructor for type not found (View Component)

From Dev

No suitable constructor found for type when we use an Enum - Jackson JSON

From Dev

No suitable constructor found error occuring inside extended subclass Constructors

From Dev

"no suitable method found for add(java.lang.String)"in arraylist constructor?

From Dev

Android no suitable constructor found for HeaderItem(int,String,<null>)

From Dev

How do I fix a Java:26: error: No suitable constructor found

From Dev

No suitable constructor found for type when we use an Enum - Jackson JSON

From Dev

No suitable constructor found for TreeMap with Comparator<Map.Entry

From Dev

JavaFX : Retrieving Image from server, no suitable constructor found

From Dev

no suitable constructor found for JsonObjectRequest error when using volley

Related Related

  1. 1

    JsonMappingException: No suitable constructor found for type

  2. 2

    JsonMappingException: No suitable constructor found for type -- for an external object

  3. 3

    JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

  4. 4

    JsonMappingException: No suitable constructor found for type [simple type, class car.Car$Parts]

  5. 5

    Deserializing Guava's Table with Jackson throws "JsonMappingException: No suitable constructor found for type"

  6. 6

    com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of : poja class no suitable constructor found

  7. 7

    Deserializing Guava's Table with Jackson throws "JsonMappingException: No suitable constructor found for type"

  8. 8

    No Suitable Constructor Found (Java)

  9. 9

    Java no suitable constructor found

  10. 10

    No suitable constructor found for JsonObjectRequest

  11. 11

    No suitable Constructor Found

  12. 12

    No suitable constructor found for java

  13. 13

    No suitable constructor found for type GeoJsonPoint

  14. 14

    CsvParser "No suitable constructor found for type"

  15. 15

    No Suitable constructor found for File(file)

  16. 16

    No Suitable Constructor Found for Student (no arguments)

  17. 17

    No Suitable constructor found for File(file)

  18. 18

    No suitable constructor found for type GeoJsonPoint

  19. 19

    Chrome driver with Selenium : "no suitable constructor found for RemoteWebDriver"

  20. 20

    Suitable constructor for type not found (View Component)

  21. 21

    No suitable constructor found for type when we use an Enum - Jackson JSON

  22. 22

    No suitable constructor found error occuring inside extended subclass Constructors

  23. 23

    "no suitable method found for add(java.lang.String)"in arraylist constructor?

  24. 24

    Android no suitable constructor found for HeaderItem(int,String,<null>)

  25. 25

    How do I fix a Java:26: error: No suitable constructor found

  26. 26

    No suitable constructor found for type when we use an Enum - Jackson JSON

  27. 27

    No suitable constructor found for TreeMap with Comparator<Map.Entry

  28. 28

    JavaFX : Retrieving Image from server, no suitable constructor found

  29. 29

    no suitable constructor found for JsonObjectRequest error when using volley

HotTag

Archive