views:

91

answers:

1

Hi All,

I am writing an application where i have to interact with MS SQL database. In my application i am creating web services (using javax.jws) for accessing database tables. i am creating one operation(method) in web service with return type java.lang.Object[][] as follows :

@WebMethod(operationName = "get_HistoryInfoByUser")

public java.lang.Object[][] get_HistoryInfoByUser(@WebParam(name = "email_Id")
String email_Id) throws Exception{
   java.lang.Object[][] historyInfo = null;

     // some code here

  return historyInfo;
}

and for calling web service operation(method) in my application, i am writing following code:

public Object[][] get_HistoryInfoByUser(String email_Id) {

      java.util.List<net.java.dev.jaxb.array.AnyTypeArray> historyInfo = null;

    try {

        historyInfo = port.getHistoryInfoByUser(email_Id);

    } catch (Exception_Exception ex) {
        ex.printStackTrace();
    }
       return (Object[][]) historyInfo.toArray();
 }

but i am getting an Exception

Exception in thread "Thread-8" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.Object;

Web service operation return type is java.util.List(net.java.dev.jaxb.array.AnyTypeArray) and i need return type java.lang.Object[][].

Please can you give me any suggestion, which will help me to overcome with this problem.

+1  A: 

Casting is not used for transforming objects, it's used for reclassifying the type of an object. It is there to tell the compiler that a variable should be treated like a different class. If you try to cast a variable to some type it is not compatible with, you get the ClassCastException.

Object ojc = new ArrayList();
obj.add(new Object()); //Compiler error because Object does not have an add method
ArrayList lst = (ArrayList)obj;
lst.add(new Object()); //Works because now the compiler knows that the variable is an ArrayList
MyClass myClass = (MyClass)obj; //ClassCastException because the object is not actually a MyClass object
myClass.add(new Object()); //Assuming MyClass defines and add method that takes and object, this line compiles

In your case, Object[] and Object[][] are completely different types and not something that you can cast between. Instead, you need to first make the 2D array and then set your array to be the first member of the 2D array.

Object[][] result = new Object[1][];
result[0] = historyInfo.toArray();
unholysampler