Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A java.lang.ClassCastException while accessing web service method written in java. jaxb

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.

like image 285
Toman Avatar asked Sep 15 '10 10:09

Toman


People also ask

How do you solve ClassCastException in Java?

To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

What causes ClassCastException in Java?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

What is Java Lang ClassCastException?

Class ClassCastExceptionThrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException : Object x = new Integer(0); System. out. println((String)x);

What is ClassCastException in Java with example?

It is a runtime exception that occurs when the application code attempts to cast an object to another class of which the original object is not an instance. For example, a String object cannot be cast to an Integer object and attempting to do so will result in a ClassCastException .


1 Answers

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();
like image 111
unholysampler Avatar answered Nov 02 '22 19:11

unholysampler