Solved

Webservice object that contains Objects

Posted on 2006-07-18
21
583 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
 

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:mayankeagle
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 500 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:mayankeagle
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now