无法将ArrayOfObjects强制转换为Java.util.Collection

时间:2012-04-25 12:01:51

标签: java collections casting

我尝试用各种不同的方法进行几个小时的捣乱。

这里是Java中的代码,它在ASP .Net C#中调用Web服务方法

public DefaultTableModel SelectAllProjects ()
{
    try{
    DefaultTableModel DtM;
    Collection c = (Collection)wws.selectAllProjects();
    Iterator iter = c.iterator();
    DtM = new DefaultTableModel();

    DtM.addColumn("Project ID:");
    DtM.addColumn("Project Name:");
    DtM.addColumn("Project Description:");

    Vector vec = new Vector(c.size());

    while(iter.hasNext()){

        Project p = new Project();
        p.getPId();
        p.getPName();
        p.getPDescript();
        vec.add(p);
        DtM.addRow(vec);

    }

    return DtM;
    }
    catch(Exception e)
    {
        System.out.println(e);
        return null;
    }

}

这是webservice方法......

[WebMethod]//Ska vara collection
public List<Project> SelectAllProjects()
{
    Controller contr = new Controller();
    List<Project> project = contr.SelectAllProjects();
    return project;
}

我知道该方法正在运行,因为使用C#编写的Windows窗体应用程序可以成功调用它。

我也知道Java和Web Services之间的联系,因为我可以调用其他方法。

My Catch(Exception e)返回以下消息:

java.lang.ClassCastException: javaapp.ArrayOfProject cannot be cast to java.util.Collection

感谢您的回答,找不到任何结果。

P.S。试过Object[] obj = (Object[])wws.selectAllProjects();Project[] proj = (Project [])wws.selectAllProjects();

请求自动生成的ArrayOfProject.java类

package javaapp;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import java.util.*;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArrayOfProject", propOrder = { "project" })
public class ArrayOfProject {
    @XmlElement(name = "Project", nillable = true)
    protected List<Project> project;

    public List<Project> getProject() {
        if (project == null) {
            project = new ArrayList<Project>();
        }
        return this.project;
    }
}

代码来自我的视图类..(我按下的按钮)

private void btnAllProjects_actionPerformed(ActionEvent e) {
    try{
    Controller control = new Controller();
    DefaultTableModel DtM;
    DtM = control.SelectAllProjects();
    JTable selectProjectsTable = new JTable (DtM);
    }
    catch (Exception ex)
    {
        System.out.println(ex);
    }

}

我最新的例外情况列表:

C:\Oracle\Middleware\jdk160_24\bin\javaw.exe -client -classpath "C:\Users\MS\Desktop\Web Services\Java Uppgift 2\.adf;C:\Users\MS\Desktop\Web Services\Java Uppgift 2\JavaApp\classes;C:\Oracle\Middleware\wlserver_10.3\server\lib\weblogic.jar;C:\Oracle\Middleware\oracle_common\modules\oracle.jrf_11.1.1\jrf.jar;C:\Oracle\Middleware\modules\com.oracle.toplink_1.0.0.0_11-1-1-5-0.jar;C:\Oracle\Middleware\modules\org.eclipse.persistence_1.1.0.0_2-1.jar;C:\Oracle\Middleware\modules\com.bea.core.antlr.runtime_2.7.7.jar;C:\Oracle\Middleware\modules\javax.persistence_1.0.0.0_2-0-0.jar;C:\Oracle\Middleware\oracle_common\modules\oracle.xdk_11.1.0\xmlparserv2.jar;C:\Oracle\Middleware\oracle_common\modules\oracle.xdk_11.1.0\xml.jar;C:\Oracle\Middleware\oracle_common\modules\oracle.nlsrtl_11.1.0\orai18n-mapping.jar" -Djavax.net.ssl.trustStore=C:\Users\MS\AppData\Local\Temp\trustStore7859653733656575754.jks javaapp.Main
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2760)
    at java.util.Arrays.copyOf(Arrays.java:2734)
    at java.util.Vector.ensureCapacityHelper(Vector.java:226)
    at java.util.Vector.insertElementAt(Vector.java:554)
    at javax.swing.table.DefaultTableModel.insertRow(DefaultTableModel.java:357)
    at javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:333)
    at javaapp.Controller.SelectAllProjects(Controller.java:56)
    at javaapp.View.btnAllProjects_actionPerformed(View.java:103)
    at javaapp.View.access$200(View.java:24)
    at javaapp.View$3.actionPerformed(View.java:68)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6289)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6054)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4652)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4482)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4482)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:603)
    at java.awt.EventQueue$1.run(EventQueue.java:601)

4 个答案:

答案 0 :(得分:1)

您需要Arrays.asList方法将数组转换为List,即Collection

答案 1 :(得分:0)

班级javaapp.ArrayOfProject需要实施Collection

作为替代方法,您可以创建一个实现Collection的包装器。

最简单的可能是创建一个ArrayList,复制所有元素并使用ArrayList。

答案 2 :(得分:0)

考虑到Arrays不是集合,Arrays类上有一个名为asList的实用方法,它以数组作为参数。

答案 3 :(得分:0)

ArrayOfProject是您的SOAP框架生成的类。它只是一个普通的Java类。检查它具有哪些getter,具体取决于所使用的框架,它将返回一个数组或列表。

在您的代码中编辑此行:

Collection c = (Collection)wws.selectAllProjects();

Collection c = wws.selectAllProjects().getProject();

其余的将按原样运作。