Why is SonarQube giving a transient/private error when class is Serialized?
SonarQube marked this line as an error, because java.util.List doesn't implement java.io.Serializable. java.util.ArrayList is serializable, but the bondAxeMarkQuoteUpdates
is protected
so somebody can assign other non-serializable list to it (e.g. in a subclass).
To solve the problem you can:
- make the field as
transient
, but it will be ignored during serialization - make the field as
private
, so SonarQube can verify that nobody assigned non-serializable list to it - change the field type to serializable type (e.g. java.util.ArrayList)
I receive the same error and the solution was turn the class used on the variable as Serializable
.
For example, this show an error because Object
is not Serializable
:
private Map<String, Object> map = new HashMap<>();
The simplest solution in the case was turn the second parameter Serializable
. So, you could use:
private Map<String, Serializable> map = new HashMap<>();
If you are using your own class (instead of Object
), you can put the class to implements Serializable
.