I am using a Java Web Service for
@WebService()
public class myWebService {
/**
* Web service operation
*/
MyClass Obj ;
@WebMethod(operationName = "webmethod1")
@Oneway
public void webmethod1(@WebParam(name = "serailNo") String serailNo) {
obj = new MyClass();
//do some operations on obj;
}
/**
* Web service operation
*/
@WebMethod(operationName = "webmethod2")
public void webmethod2() {
//do some operations on obj after doing intial operations in web method1
}
}
I am unable to access obj in webmethod2. It is getting a null pointer exception.
As a detail : I want to create a object . That object should be accessed across all web methods. In such a way webmethod1 will do intial operations on obj and followed web method2 will use the same obj.
How can i achieve this
The reason you are getting a nullpointer is because the webservice is not stateful, i.e the object does not exist during the second call. It is possible to create a stateful webservice but that depends on the kind of webservice and server your running it on…
As npinti mentioned you could send the object along with the service. Although that might not be favorable, e.g if the object is big.
You could create a local cache on the server containing all the created objects by and mapping them with the serialNo.