on 09-05-2007 11:54 AM
Dear All,
I am developing a Web service project where I am using the holder classes(StringHolder, LongHolder, etc) in my web service functions as parameters.
eg:
Void myFunction(StringHolder InOut, LongHolder outResult)
{
// assign some new values to Inout and outResult
}
My problem is that....after the client calls the function myFunction()
the values of Inout and outResult are the same as the initial
values and not the newly assigned values..
I am using SAP NetWeaver Developer Studio
Version: 7.0.09 , SAP netweaver 2004s
Any help?
Thanks,
Talimeren
Talimeren,
Can you please post more example code, i.e. the implementation of "myFunction"?
How are you setting the values of the holder classes? You cannot change the object reference of a parameter inside a function, you have to change the actual value inside the holder object.
Best regards,
Daniel
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just to make sure I am not going crazy I tried the following code:
public class Playground {
public static void main(String[] args) {
StringHolder sh1 = new StringHolder("Original Value");
LongHolder lh1 = new LongHolder(2L);
System.out.println(sh1.value + " " + lh1.value);
Playground p = new Playground();
p.myFunction(sh1, lh1);
System.out.println(sh1.value + " " + lh1.value);
}
public void myFunction(StringHolder InOut, LongHolder outResult){
// do something
InOut.value="Updated value";
outResult.value=1L;}
}
This is the output I get:
<i>Original Value 2
Updated value 1</i>
When I posted that the problem is somewhere else in your code, I meant regarding the way you are calling the function or what you are doing immediately after or before it. You could try enabling Assert and placing an assert right after your call to myFunction to check the values there?
Hi Hermann Hans thanks for your reply
But I guess you haven't read my first post clearly.
I am developing a web service project
myFunction() is a web service function in that.
When I am calling for the client side I am not getting the
modified values of the holders. Between the web service functions
I am getting the modified values.
eg:
public class service
{
public void myFunction(StringHolder InOut, LongHolder outResult)
{
// do something
InOut.value="Updated value";
outResult.value=1;
}
void operation()
{
StringHolder sh1 = new StringHolder("Original Value");
LongHolder lh1 = new LongHolder(2);
myFunction(sh1, lh1);
//
//here I am getting the Updated value and Long value as 1;
}
}
but the problem is when the client calls the webservice. ..
I am not getting the updated values
thanks,
talimeren
Hopefully, I'm understanding what your issue is. I think it's the same thing that I was having trouble with recently. I was always under the assumption that Java did "pass by Reference" but was surprised that it does not.
If you want to get the modified value back you need to return a String from your method. If you need to return several values back from your method, I believe you're going to have to create a custom object that contains the properties you need, populate the object and then return that object.
Try this simple code (no comments please. I just threw it together) to see what I'm saying.
Hope that helps.
public class TestPassByRef {
public static void main(String[] args) {
String mainVal = "originalVal";
TestPassByRef tp = new TestPassByRef();
tp.changeIt1(mainVal);
System.out.println("After changeIt1 val ='" + mainVal + "'");
mainVal = "originalVal";
mainVal = tp.changeIt2(mainVal);
System.out.println("After changeIt2 val ='" + mainVal + "'");
}
private void changeIt1(String val) {
System.out.println("changeIt1: coming in value is '" + val + "'");
val = "newVal";
return;
}
private String changeIt2(String val) {
System.out.println("changeIt2: coming in value is '" + val + "'");
val = "newVal";
return val;
}
}
Hello David,
I think we've already gotten to that point and we've agreed that his code should execute correctly assuming you are running it locally. However, the problem seems to be that while the code executes fine on the server (or when run locally), as soon as he runs it from a client which makes a call to the web service, the values are not changed when the function returns.
Maybe someone smarter than me could explain where the two objects are located that are being modified by myFunction? The server or the client? Possibly I am thinking about this the wrong way, but how is pass-by-reference (ok, ok, pass by value of the reference) implemented in this scenario?
Could it be the case that the client passes an address to the server (the value of the reference to the object sitting on the client) with which the server cannot do anything with?
Hello all,
Java always passes objects by references and primitives by value. However, if you modify the reference itself this will not be reflected in the calling method. Only changes in the object itself. This is the reason why David got the results in his test-code: With val = "newVal" you were changing the reference of the val object, but this wasn't reflected in the calling method.
Talimeren, did you try to attach a debugger to the NetWeaver yet? Can you see the values changing in the debugger or maybe the method isn't called at all? Are you using org.omg.CORBA.StringHolder?
Best regards,
Daniel
Hi Daniel Beitler thanks for your reply.
I am not sure whether you understood the problem clearly or not.
Anyway about the debugger in the netweaver, I haven't tried attaching any.
Actually I know that my function is executing properly.
I have tried out with the following example :
String mySecondFunction(StringHolder outReturn)
{
String result="Before Assignment";
outReturn.value="updated value";
result=Result+"After Assignment";
return result;
}
And I am getting the returned value of result as Before Assignment After Assignment
but my outReturn is not updated at all.
I am using javax.xml.rpc.holders.StringHolder
thanks,
talimeren
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.