Link to home
Start Free TrialLog in
Avatar of kristian_gr
kristian_grFlag for Norway

asked on

Webservice object that contains Objects

Hi.
I have two simple classes, attatched at the bottom. (Theese are example classes.)
Class test holds two strings, and class sample holds one String and an Array og class test.

Now, when I try to use this in a webservice all I get is an: Internal Server Error (serialization error: java.lang.NullPointerException)

Anyone how sees what I do wrong?


public class test {
   private String a, b;
   pulibc test(String a, String b) {
        this.a = a;
        this.b = b;
   }
   public String getA() { return this.a; }
   public String getB() { return this.b; }
}
public class sample {
    private String a;
    private test[] b;
    pulbic sample(String a) {
       this.a = a;
   }
   public String getA() {
       return this.a;
   }
   public test[] getTest() {
       test t = new test("hello");
       b[0] = t;
      return b;
   }
}

Avatar of girionis
girionis
Flag of Greece image

Hi kristian_gr

how are you trying to use it? Can you post the code you call it?

Cheers
Avatar of kristian_gr

ASKER

   /**
     * Web service operation
     */
    public sample getsimple(java.lang.String name, java.lang.String text) {
        return new simpleTest(text);
    }
WHat is the simpleTest?
Sorry. the correct is:
/**
     * Web service operation
     */
    public sample getsimple(java.lang.String name, java.lang.String text) {
        return new sample(name, text);
    }
Or maybe you need to Serialize the objects that you are passing through the wire. Try implement Serializable in your test and sample classes:

public class test implements Serializable...

public class sample implements Serializable...
kristian_gr,
> return new sample(name, text);

This shouldn't compile as sample ctor only takes one argument (string a). Are you sure there is not another sample class somewhere else?
ALL of the objects transported through web services MUST be serializable (this includes ALL agregated objects and is tranzitive).
Yes they have to be serializable. To generate the proxies of the web-service, use WSDL2Java from Apache Axis and it'll make life easier. You just need to create objects of the stubs and call its methods like you do for regular objects and it will result in calls to the web-service.

http://ws.apache.org/axis/java/user-guide.html

http://ws.apache.org/axis/java/apiDocs/org/apache/axis/wsdl/WSDL2Java.html 
ok, this is a code that compiles:
public class test implements Serializable {
    private String a;
    private String b;
    public test() { }
    public test(String a, String b) { this.a = a; this.b = b; }
    public String getA() { return a;  }
    public void setA(String a) { this.a = a; }
    public String getB() { return b; }
    public void setB(String b) { this.b = b; }
}
public class sample implements Serializable{
    private String a;
    private test[] testing;

    public sample() {    }
    public sample(String a) { this.a = a; }
    public String getA() { return a; }
    public void setA(String a) { this.a = a; }
    public test[] getTesting() {
        test t = new test(this.a, "hello");
        testing[0] = t;
        return this.testing;
    }
    public void setTesting(test[] testing) { this.testing = testing; }
}
    /**
     * Web service operation
     */
    public sample getSample(java.lang.String a) {
        return new sample(a);
    }

But still I get this error:
 InvocationTargetException org.netbeans.modules.websvc.registry.ui.ReflectionHelper.callMethodWithParams(ReflectionHelper.java:449) org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.invokeMethod(TestWebServiceMethodDlg.java:442) org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.access$500(TestWebServiceMethodDlg.java:64) org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg$4.run(TestWebServiceMethodDlg.java:385) org.openide.util.Task.run(Task.java:189) org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:330) org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:721) Next Exception Layer null sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:585) org.netbeans.modules.websvc.registry.ui.ReflectionHelper.callMethodWithParams(ReflectionHelper.java:443) org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.invokeMethod(TestWebServiceMethodDlg.java:442) org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.access$500(TestWebServiceMethodDlg.java:64) org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg$4.run(TestWebServiceMethodDlg.java:385) org.openide.util.Task.run(Task.java:189) org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:330) org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:721) Next Exception Layer JAXRPC.TIE.04: Internal Server Error (serialization error: java.lang.NullPointerException) com.sun.xml.rpc.client.StreamingSender._raiseFault(StreamingSender.java:497) com.sun.xml.rpc.client.StreamingSender._send(StreamingSender.java:294) webservice.TestWSSEI_Stub.getSample(TestWSSEI_Stub.java:172) webservice.TestWSClient.testwsseiportGetSample(TestWSClient.java:26) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:585) org.netbeans.modules.websvc.registry.ui.ReflectionHelper.callMethodWithParams(ReflectionHelper.java:443) org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.invokeMethod(TestWebServiceMethodDlg.java:442) org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.access$500(TestWebServiceMethodDlg.java:64) org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg$4.run(TestWebServiceMethodDlg.java:385) org.openide.util.Task.run(Task.java:189) org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:330) org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:721)
Try with a very simple "sample" class and see if it works:

public class sample implements Serializable
{
    private String a;
    public sample(String a) { this.a = a; }
    public String getA() { return a; }
    public void setA(String a) { this.a = a; }
}
Yes that works.
And this works
public class sample implements Serializable{
    private String a;
    private test[] testing;

    public sample() {    }
    public sample(String a) { this.a = a; }
    public String getA() { return a; }
    public void setA(String a) { this.a = a; }
    public test[] getTesting() {
        test t = new test(this.a, "hello");
        //testing[0] = t;
        return this.testing;
    }
    public void setTesting(test[] testing) { this.testing = testing; }
}

So it is

> testing[0] = t;

that's causing the problem. Try to use a Vector instead

public class sample implements Serializable{
    private String a;
    private Vector <test> testing;

    public sample() {    }
    public sample(String a) { this.a = a; }
    public String getA() { return a; }
    public void setA(String a) { this.a = a; }
    public test[] getTesting() {
        test t = new test(this.a, "hello");
        testing.addElement(test);
        return this.testing;
    }
    public void setTesting(test[] testing) { this.testing = testing; }
}
Ok I see the problem now, you need to initialize your array :)

private test[] testing = new test[1];
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note that you can only add a certain amount of element into an array, if you want dynamic expansion you will need to use a Collections object, like Vector or ArrayList

public class sample implements Serializable{
    private String a;
    private Vector <test> testing = new Vector<test>();

    public sample() {    }
    public sample(String a) { this.a = a; }
    public String getA() { return a; }
    public void setA(String a) { this.a = a; }
    public test[] getTesting() {
        test t = new test(this.a, "hello");
        testing.addElement(test);
        return this.testing;
    }
    public void setTesting(test[] testing) { this.testing = testing; }
}
tnx.
but:
   public test[] getTesting() {
        test t = new test(this.a, "hello");
        testing.addElement(test);
        return this.testing;
    }
don't compile.
I've done this and it seams to work:

    public test[] getTestElement() {
        test t = new test(this.a, "hello");
        this.testing.addElement(t);
        test[] a = new test[this.testing.size()];
        for(int i = 0; i > this.testing.size(); i++) {
            a[i] = this.testing.get(i);
        }
        return a;
    }

Any quicker way using 1.5?
> don't compile.

Yes you are right, my mistake, you should return either the vector itself or an element. You could do:

public Vector getTesting() {
        test t = new test(this.a, "hello");
        testing.addElement(test);
        return this.testing;
    }

which will return the whole vector (with all the tests in there).
Avatar of Webstorm
Webstorm

>> Any quicker way using 1.5?
Yes , and earlier versions :

   public test[] getTesting() {
        test t = new test(this.a, "hello");
        testing.addElement(test);
        return (test[])this.testing.toArray(new test[testing.size()]);
    }
Of course the set method should change as well:

> public void setTesting(test[] testing) { this.testing = testing; }

public void setTesting(Vector<test> testing) { this.testing = testing; }
>> test t = new test(this.a, "hello");

Is test serializable?
It is

> public class test implements Serializable {