?
Solved

Webservice object that contains Objects

Posted on 2006-07-18
21
Medium Priority
?
598 Views
Last Modified: 2008-01-09
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;
   }
}

0
Comment
Question by:kristian_gr
  • 12
  • 5
  • 2
  • +2
21 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17128296
Hi kristian_gr

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

Cheers
0
 

Author Comment

by:kristian_gr
ID: 17128310
   /**
     * Web service operation
     */
    public sample getsimple(java.lang.String name, java.lang.String text) {
        return new simpleTest(text);
    }
0
 
LVL 35

Expert Comment

by:girionis
ID: 17128318
WHat is the simpleTest?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kristian_gr
ID: 17128325
Sorry. the correct is:
/**
     * Web service operation
     */
    public sample getsimple(java.lang.String name, java.lang.String text) {
        return new sample(name, text);
    }
0
 
LVL 35

Expert Comment

by:girionis
ID: 17128328
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...
0
 
LVL 35

Expert Comment

by:girionis
ID: 17128334
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?
0
 
LVL 12

Expert Comment

by:enachemc
ID: 17128387
ALL of the objects transported through web services MUST be serializable (this includes ALL agregated objects and is tranzitive).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 17128415
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 
0
 

Author Comment

by:kristian_gr
ID: 17128489
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)
0
 
LVL 35

Expert Comment

by:girionis
ID: 17128593
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; }
}
0
 

Author Comment

by:kristian_gr
ID: 17128612
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; }
}

0
 
LVL 35

Expert Comment

by:girionis
ID: 17128646
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; }
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 17128665
Ok I see the problem now, you need to initialize your array :)

private test[] testing = new test[1];
0
 
LVL 35

Accepted Solution

by:
girionis earned 2000 total points
ID: 17128676
public class sample implements Serializable{
    private String a;
    private test[] testing = new test[1];

    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; }
}

I should have seen it earlier :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 17128681
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; }
}
0
 

Author Comment

by:kristian_gr
ID: 17128757
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?
0
 
LVL 35

Expert Comment

by:girionis
ID: 17128784
> 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).
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17128796
>> 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()]);
    }
0
 
LVL 35

Expert Comment

by:girionis
ID: 17128802
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; }
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 17128923
>> test t = new test(this.a, "hello");

Is test serializable?
0
 
LVL 35

Expert Comment

by:girionis
ID: 17128940
It is

> public class test implements Serializable {
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question