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:
o2
from your code?o2
but do not add it to list
?a
field from the POJO
class?a
into a single element array (a = new int[1]
)?a
from 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: String
or java.util.Date
)?o1
and o2
inside the list (i.e., add(o2) and then add(o1)) ?POJO
parameter (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 o2
is omitted then I keep it away, proceeding with a smaller program that is more easy to understand.