Anna
Anna

Reputation: 889

Failed to instantiate java.util.Set using constructor NO_CONSTRUCTOR with arguments

I have this code:

@NoArgsConstructor
public class localizedInformations implements Serializable {

    @Getter
    @JsonProperty("infos")
    private Map<String, Set<Info>> localizedInfos = new HashMap<>();
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TechnicalInfo implements Serializable {

    private static final long serialVersionUID = -8926217088092761683L;

    private String label;

    private List<String> values;
}

At some point at my execution when I do a findAll mongo operation, I get this error:

Failed to instantiate java.util.Set using constructor NO_CONSTRUCTOR with arguments

Can you see where is the problem?

Thanks!

Upvotes: 1

Views: 7626

Answers (2)

Farhad
Farhad

Reputation: 632

here is an example

private Map<String, Set<Info>> localizedInfos = new HashMap<>();

the problem with this code is that, when you save the data no error comes up. then when you are retrieving data, (as Zsolt V explained) java tries to build that data again, but because the type of that object is an interface, java cant decide what kind of Set that is (it can be HashSet, LinkedHashSet and etc.)

one simple solution is that you use HashSet Instead Of Set like this

private Map<String, HashSet<Info>> localizedInfos = new HashMap<>();

I had this problem when I was trying to save a Key object form Spring security package and it was and interface

GoodLuck Coding Guys.

Keep The Stack Flowing :D

Upvotes: 5

Zsolt V
Zsolt V

Reputation: 517

When you call an instance of localizedInformations from database your tools try to build up it from the data collected from database and from the structure defined by your class. You wrote Set and the process will try to instantiate a Set (which is impossible since it is an interface) and the process won't try to guess what kind of actual implementation you would like to use. So you should specify an actual Set implementation for your member.

Upvotes: 2

Related Questions