Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Webservice object that contains Objects

Posted on 2006-07-18
21
Medium Priority
?
596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:
Suggested Courses

618 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