AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-basics: Fields in a serializable class should either be transient or serializable

I´m getting error as "Make "daaDistanceList(DAOInfo class)" transient or serializable". But I´ve implemented Serializable and made variables as Private.Please suggest how to solve this sonar issue.

public class RankMap extends HashMap> implements Serializable {
private static final long serialVersionUID = 1L;
private PRequest pRequest = null;
private List dAAInfoList = new ArrayList();
public RankMap(PRequest pRequest)
{
public RankMap(PRequest pRequest)
{
this.pRequest= pRequest;
init();
}
// create Garage Info List
private void init()
{
Integer grgPK = 0;

for (Tech tech : pRequest.getTechList())
{
grgPK = tech.getTechGrgPK();

if (!this.containsKey(grgPK))
{
DAOInfo grgDAAInfo = new DAOInfo(pRequest);
grgDAAInfo.setGrgPK(grgPK);
grgDAAInfo.init();
garageDAAInfoList.add(grgDAAInfo);
this.put(grgDAAInfo.getGrgPK(), grgDAAInfo.getDaaRankMap());
}
}
}
class DAOInfo implements Serializable
{
private static final long serialVersionUID = 1L;
private int grgPK = 0;
private double grgLatitude = 0;
private double grgLongitude = 0;
private PRequest pRequest = null;
private List<DistanceInfo> daaDistanceList = null;
private HashMap<String,Integer> daaRankMap = null;

public DAOInfo(PRequest pRequest)
{
daaDistanceList = new ArrayList<DistanceInfo>();
daaRankMap = new HashMap<String,Integer>();
this.pRequest = pRequest;
}

}

class DistanceInfo implements Serializable
{
private static final long serialVersionUID = 1L;
private String daa = null;
private double distance = 0;

DistanceInfo(String daa, double distance)
{
this.daa = daa;
this.distance = distance;
}

public String getDaa() {
return daa;
}

public Double getDistance() {
return distance;
}

}
}

corejava x 348
basics x 169
Posted On : 2016-08-24 10:48:20.0
profile veera - anyforum.in veera
3220
up-rate
2
down-rate

Answers


It happens if:
* A public method return a collection implementation class
* A public method accept a collection implementation class as an input
* The type of a public field is a collection implementation class

Change private List<DistanceInfo> daaDistanceList to implementation collection class as private ArrayList<DistanceInfo> daaDistanceList;

and if you have any public setter method as:

public void setMyList(ArrayList<String> newList) {
//not allowed - S1319 will fire
}

That means you have to cast the value from a List to an ArrayList.

public void setMyList(List<String> newList) {
myList = (ArraryList<String>) newList;
}


SerializableCollection<String> a = new ArrayList<String>();

This won´t however compile. Even if ArrayList<String> implements both Collection<? extends Serializable> and Serializable, the class does not implements SerializableCollection<String>.

Now, you can if you would, circumvent even this problem by declaring a new class:

SerializableArrayList<T extends Serializable> extends ArrayList<T> implements SerializableCollection<T> {}

Now you have essentially combined everything you need and would be able to fulfill the original requirement:

SerializableCollection<String> a = new SerializableArrayList<String>();

Since the Serializable marker is just a non-formal ´label´, ensuring that both your collection and its content implement Serializable still does not guarantee that the collection and its content actually can be serialized.

Posted On : 2016-08-25 00:22:27
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523186220845
Reply This Thread
up-rate
3
down-rate
Comments
Thanks Raj for your reply.But i dont have any public setters and i tried wih implementing writeobj() and readobj () with default serialization.Issue got solved but is this a correct approach or not and do we need to implement two methods or WriteObj() is sufficient?

private void writeObject(ObjectOutputStream outPutStream) throws IOException {
//perform the default serialization for all non-transient, non-static fields
outPutStream.defaultWriteObject();
}

private void readObject(ObjectInputStream inputStream) throws ClassNotFoundException, IOException {
//always perform the default de-serialization first
inputStream.defaultReadObject();
}
profile veera - anyforum.in veera
32  2  0
Posted On :2016-08-25 11:18:09.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in