Solved

Webservice object that contains Objects

Posted on 2006-07-18
21
585 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
github account with ecipse 1 48
eclipse argument 14 61
MySQL  on Tomcat 8 47
Why my table column Id is not passed to java object? 4 39
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

831 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