I have the following problem:
class POJO implements Serializable{
int[] a = new int[2];
int b;
}
Server:
printThere(ArrayList<POJO> list) throws RemoteException{
for(POJO obj:list)
System.out.println(obj.a[0]+" "+obj.a[1]+" "+obj.b);\\<-----THERE LINE
}
Client:
main(){
POJO o1 = new POJO();
o1.a[0] =1;
o1.a[1] =2;
o1.b= 11;
POJO o2 = new POJO();
o2.a[0] =10;
o2.a[1] =20;
o2.b= 111;
ArrayList<POJO> list = new ArrayList<POJO>();
list.add(o1);
list.add(o2);
for(POJO obj:list)
System.out.println(obj.a[0]+" "+obj.a[1]+" "+obj.b);\\<-----HERE LINE
server = proxy.lookup("server");
server.printThere(list);
}
Here the lines print as follows:
1 2 11
10 20 111
There the lines print as follows:
1 2 111
10 20 111
Why am I losing the value of b in one object? I tried using a Vector<POJO> and a POJO[] in the remote method but got the same result.
I don’t see the problem. That said, I think you will be able to get to the bottom of it quite quickly. Here’s my suggestion:
Gradually omit pieces from your program. After each step run the code and see if the problem persists. Each such “experiment” will give you valuable details about the cause of the problem. Eventually you want to be able to detect what element in your code is causing the problem.
Here are some steps to take along the way:
o2from your code?o2but do not add it tolist?afield from thePOJOclass?ainto a single element array (a = new int[1])?afrom an array to a (say) Integer?b‘s type from int to some other primitive?b‘s type from int to some other non-primitive type (say:Stringorjava.util.Date)?o1ando2inside the list (i.e., add(o2) and then add(o1)) ?POJOparameter (instead of a single ArrayList)?Usually, after applying several “transformation” steps such as these, I gather enough information to identify the root cause. On top of that I try to reduce the scope. If I find that the problem occurs even if
o2is omitted then I keep it away, proceeding with a smaller program that is more easy to understand.